<?php
/**
 * Web-based Migration Runner: Add is_active to categories table
 * Access this file in your browser to run the migration
 */

// Bootstrap the application
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/database.php';

header('Content-Type: text/html; charset=utf-8');

?>
<!DOCTYPE html>
<html>
<head>
    <title>Add is_active to Categories - TellerPro</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            margin-top: 0;
        }
        .success {
            color: #16a34a;
            font-weight: bold;
            font-size: 18px;
        }
        .error {
            color: #dc2626;
            font-weight: bold;
            font-size: 18px;
        }
        .info {
            color: #2563eb;
        }
        pre {
            background: #f3f4f6;
            padding: 15px;
            border-radius: 4px;
            overflow-x: auto;
            margin: 20px 0;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #2563eb;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-top: 20px;
        }
        .btn:hover {
            background: #1d4ed8;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Add is_active Column to Categories</h1>
        
        <?php
        try {
            echo '<p class="info">Running migration...</p>';
            
            $sql = file_get_contents(__DIR__ . '/../database/migrations/add_is_active_to_categories.sql');
            
            if ($sql === false) {
                die('<p class="error">Error: Could not read migration file.</p>');
            }
            
            $db = db();
            $db->exec($sql);
            
            echo '<p class="success">✓ Migration completed successfully!</p>';
            echo '<p><strong>Changes made:</strong></p>';
            echo '<ul>';
            echo '<li>Added is_active column to categories table</li>';
            echo '</ul>';
            
            echo '<p><a href="' . APP_URL . '/stock-takings/create" class="btn">Go to Stock Taking</a></p>';
            
        } catch (PDOException $e) {
            echo '<p class="error">✗ Migration failed: ' . htmlspecialchars($e->getMessage()) . '</p>';
            echo '<p><strong>Error Details:</strong></p>';
            echo '<pre>' . htmlspecialchars($e->getMessage()) . '</pre>';
        }
        ?>
    </div>
</body>
</html>
