<?php
require_once dirname(__DIR__) . '/app/bootstrap.php';

use App\Core\Database;

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

try {
    echo "Connecting to database...\n";
    $pdo = Database::getConnection();
    
    $sqlFile = dirname(__DIR__) . '/create_saas_and_woocommerce_tables.sql';
    if (!file_exists($sqlFile)) {
        throw new \Exception("SQL file not found at: " . $sqlFile);
    }
    
    echo "Reading SQL file...\n";
    $sql = file_get_contents($sqlFile);
    
    echo "Executing SQL statements...\n";
    $pdo->exec($sql);
    
    echo "Migration completed successfully!\n";
    
    // Verify tables
    $stmt = $pdo->query("SHOW TABLES");
    $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
    echo "Current database tables:\n";
    foreach ($tables as $t) {
        echo "- $t\n";
    }
    
} catch (\Exception $e) {
    echo "Migration failed: " . $e->getMessage() . "\n";
}
