<?php
/**
 * Multi-Pin System Setup
 */

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

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

try {
    $database = new Database();
    $db = $database->getConnection();
    
    echo "<!DOCTYPE html>
    <html lang='ar' dir='rtl'>
    <head>
        <meta charset='UTF-8'>
        <title>Multi-Pin System Setup</title>
        <style>
            body { font-family: Arial; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; }
            .container { max-width: 800px; margin: 0 auto; }
            .box { background: white; padding: 20px; margin: 10px 0; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); }
            .success { background: #10b981; color: white; }
            .error { background: #ef4444; color: white; }
            h1 { color: white; text-align: center; margin-bottom: 30px; }
        </style>
    </head>
    <body>
    <div class='container'>
    <h1>🎛️ Multi-Pin System Setup</h1>";
    
    $sql = file_get_contents(__DIR__ . '/create_pins_system.sql');
    $statements = array_filter(array_map('trim', explode(';', $sql)));
    
    $success = 0;
    $errors = 0;
    
    foreach ($statements as $statement) {
        if (empty($statement) || strpos($statement, '--') === 0) continue;
        
        try {
            $db->exec($statement);
            $success++;
            
            if (preg_match('/CREATE TABLE.*?`(\w+)`/i', $statement, $matches)) {
                echo "<div class='box success'>✅ Table created: {$matches[1]}</div>";
            } elseif (preg_match('/INSERT INTO.*?`(\w+)`/i', $statement, $matches)) {
                echo "<div class='box success'>✅ Sample data inserted into: {$matches[1]}</div>";
            }
        } catch (PDOException $e) {
            $errors++;
            echo "<div class='box error'>❌ Error: " . htmlspecialchars($e->getMessage()) . "</div>";
        }
    }
    
    echo "<div class='box success' style='text-align:center;padding:30px;'>
        <h2 style='color:white;margin:0 0 20px 0;'>🎉 Setup Complete!</h2>
        <p style='font-size:18px;margin-bottom:20px;'>✅ Success: {$success} | ❌ Errors: {$errors}</p>
        <a href='../admin/multi_pin_dashboard.php' 
           style='display:inline-block;background:white;color:#10b981;padding:15px 40px;
                  border-radius:10px;text-decoration:none;font-weight:bold;font-size:18px;'>
        🚀 Open Multi-Pin Dashboard
        </a>
    </div>";
    
    echo "</div></body></html>";
    
} catch (Exception $e) {
    echo "<div class='box error'>❌ Fatal Error: " . htmlspecialchars($e->getMessage()) . "</div>";
}
?>
