<?php
/**
 * Migration Runner - Run POS suspend migration via browser
 * Access this file in your browser: http://your-domain/run_migration.php
 */

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';

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

echo '<!DOCTYPE html>
<html>
<head>
    <title>Running POS Migration</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin: 0 auto; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        pre { background: #f5f5f5; padding: 10px; border-radius: 5px; }
    </style>
</head>
<body>
    <h1>POS Suspend Feature Migration</h1>';

try {
    echo '<p class="info">Starting migration...</p>';

    // Modify status column to include suspended
    $sql = "
        ALTER TABLE sales
        MODIFY COLUMN status ENUM('suspended','completed','cancelled','refunded') DEFAULT 'completed'
    ";

    db()->exec($sql);
    echo '<p class="success">✓ Added "suspended" to sales.status</p>';

    // Add suspended_at column
    try {
        $sql = "
            ALTER TABLE sales
            ADD COLUMN suspended_at TIMESTAMP NULL AFTER created_at
        ";
        db()->exec($sql);
        echo '<p class="success">✓ Added suspended_at column to sales table</p>';
    } catch (Exception $e) {
        // Column might already exist
        if (strpos($e->getMessage(), 'duplicate column') !== false) {
            echo '<p class="info">⊘ suspended_at column already exists</p>';
        } else {
            throw $e;
        }
    }

    echo '<h2 class="success">Migration completed successfully!</h2>';
    echo '<p><a href="index.php">Return to application</a></p>';

} catch (Exception $e) {
    echo '<h2 class="error">Migration failed</h2>';
    echo '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
    echo '<p>Please check your database configuration and try again.</p>';
}

echo '</body>
</html>';
