<?php
/**
 * Gmind Institute — Demo Class Setup Script
 * 
 * Run this ONCE on your server to:
 * 1. Create the demo_class_requests database table
 * 2. Verify the table was created
 * 
 * Usage: Upload this file to /gmind/setup_demo_class.php
 *        Then visit https://advaysingh.co.in/gmind/setup_demo_class.php
 *        DELETE this file after setup is complete!
 */

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

try {
    $db = getDb();
    
    // Create table
    $createSQL = "
        CREATE TABLE IF NOT EXISTS `demo_class_requests` (
            `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
            `student_name` VARCHAR(255) NOT NULL,
            `email` VARCHAR(255) NOT NULL,
            `contact` VARCHAR(20) NOT NULL,
            `batch` VARCHAR(100) DEFAULT NULL,
            `course_id` INT UNSIGNED DEFAULT NULL,
            `demo_date` DATE NOT NULL,
            `status` ENUM('pending','contacted','scheduled','completed','cancelled') NOT NULL DEFAULT 'pending',
            `admin_note` TEXT DEFAULT NULL,
            `is_read` TINYINT(1) NOT NULL DEFAULT 0,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            KEY `idx_status` (`status`),
            KEY `idx_is_read` (`is_read`),
            KEY `idx_demo_date` (`demo_date`),
            KEY `idx_course_id` (`course_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ";
    
    $db->exec($createSQL);
    
    // Verify
    $stmt = $db->query('SELECT COUNT(*) as cnt FROM `demo_class_requests`');
    $row = $stmt->fetch();
    
    echo '<h1 style="color:green;font-family:Arial;">✅ Setup Complete!</h1>';
    echo '<p style="font-family:Arial;">The <code>demo_class_requests</code> table has been created successfully.</p>';
    echo '<p style="font-family:Arial;">Current rows: ' . (int)($row['cnt'] ?? 0) . '</p>';
    echo '<hr>';
    echo '<p style="font-family:Arial;color:red;"><strong>⚠️ IMPORTANT:</strong> Delete this file (<code>setup_demo_class.php</code>) from your server now!</p>';
    echo '<p style="font-family:Arial;"><a href="' . (defined('BASE_PATH') ? BASE_PATH : '/gmind') . '/">← Back to Homepage</a></p>';
    
} catch (Throwable $e) {
    echo '<h1 style="color:red;font-family:Arial;">❌ Setup Failed</h1>';
    echo '<p style="font-family:Arial;">Error: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8') . '</p>';
    echo '<p style="font-family:Arial;">Make sure your database connection in config/database.php is working.</p>';
}
