<?php
/**
 * Migration Runner: Add cost column to products table
 * Run this script to add the missing cost column
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

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 Cost to Products Migration - 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; }
        .error { color: #dc2626; font-weight: bold; }
        .info { color: #2563eb; }
        pre { background: #f3f4f6; padding: 15px; border-radius: 4px; overflow-x: auto; margin: 10px 0; }
        .section { margin: 20px 0; padding: 15px; background: #f9fafb; border-radius: 4px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Add Cost to Products Migration</h1>
        
        <div class="section">
            <h3>Running Migration...</h3>
            <?php
            try {
                $sql = file_get_contents(__DIR__ . '/../database/migrations/add_cost_to_products.sql');
                
                if ($sql === false) {
                    echo '<p class="error">✗ Error: Could not read migration file.</p>';
                    exit;
                }
                
                $db = db();
                $db->exec($sql);
                
                echo '<p class="success">✓ Cost column added to products table successfully!</p>';
                echo '<p class="info">You can now access the stock taking count page.</p>';
                echo '<p><a href="' . APP_URL . '/stock-takings/count/1" class="btn btn-primary">Go to Stock Taking Count</a></p>';
                
            } catch (PDOException $e) {
                echo '<p class="error">✗ Migration failed: ' . htmlspecialchars($e->getMessage()) . '</p>';
                echo '<pre>' . htmlspecialchars($e->getTraceAsString()) . '</pre>';
            }
            ?>
        </div>
    </div>
</body>
</html>
