<?php
require_once '../config.php';
echo "<h2>SMS Panel V2 Schema Migration</h2><pre>";
$ok = [];
$err = [];

$alters = [
    "ALTER TABLE sms_templates ADD COLUMN owner_type ENUM('admin','user') DEFAULT 'admin'",
    "ALTER TABLE sms_templates ADD COLUMN owner_id INT DEFAULT NULL",
    "ALTER TABLE sms_templates ADD COLUMN category VARCHAR(100) DEFAULT NULL",
];
foreach ($alters as $s) {
    try {
        $pdo->exec($s);
        $ok[] = "✅ $s";
    } catch (PDOException $e) {
        if (strpos($e->getMessage(), 'Duplicate') !== false)
            $ok[] = "⏭ exists";
        else
            $err[] = "❌ " . $e->getMessage();
    }
}

$tables = [
    "CREATE TABLE IF NOT EXISTS ai_settings (
        id INT AUTO_INCREMENT PRIMARY KEY, provider VARCHAR(30) NOT NULL,
        api_key TEXT NOT NULL, model_name VARCHAR(100) DEFAULT NULL,
        status ENUM('active','inactive') DEFAULT 'active',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        UNIQUE KEY unique_provider (provider)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
    "CREATE TABLE IF NOT EXISTS ai_training (
        id INT AUTO_INCREMENT PRIMARY KEY, business_name VARCHAR(200) NOT NULL,
        business_description TEXT NOT NULL, business_type VARCHAR(100) DEFAULT NULL,
        target_audience TEXT DEFAULT NULL, brand_tone VARCHAR(100) DEFAULT 'professional',
        key_products TEXT DEFAULT NULL, special_instructions TEXT DEFAULT NULL,
        status ENUM('active','inactive') DEFAULT 'active',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
    "CREATE TABLE IF NOT EXISTS ai_template_log (
        id INT AUTO_INCREMENT PRIMARY KEY, provider VARCHAR(30) NOT NULL,
        prompt TEXT NOT NULL, response TEXT NOT NULL, template_id INT DEFAULT NULL,
        created_by_type ENUM('admin','user') DEFAULT 'admin', created_by_id INT DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"
];
foreach ($tables as $s) {
    try {
        $pdo->exec($s);
        $ok[] = "✅ Table created/verified";
    } catch (PDOException $e) {
        $err[] = "❌ " . $e->getMessage();
    }
}

$defs = ['ai_default_provider' => 'gemini', 'ai_enabled' => '1'];
foreach ($defs as $k => $v) {
    try {
        $st = $pdo->prepare("INSERT IGNORE INTO settings (setting_key, setting_value) VALUES (?,?)");
        $st->execute([$k, $v]);
        $ok[] = "✅ Setting $k";
    } catch (PDOException $e) {
        $err[] = "❌ $k: " . $e->getMessage();
    }
}

foreach ($ok as $m)
    echo "$m\n";
if ($err) {
    echo "\n=== ERRORS ===\n";
    foreach ($err as $m)
        echo "$m\n";
}
echo "\n✅ Done! " . count($ok) . " ok, " . count($err) . " errors\n</pre>";
echo '<br><a href="admin/dashboard.php" style="padding:10px 20px;background:#4F46E5;color:#fff;text-decoration:none;border-radius:8px;">← Dashboard</a>';
?>