<?php
/**
 * WhatsApp Suite Setup
 * Run ONCE: https://gethostiva.com/hostiva/setup-whatsapp.php
 * DELETE AFTER USE!
 */
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "<pre>";
echo "=== Hostiva WhatsApp Suite Setup ===\n\n";

$config = require __DIR__ . '/config/config.php';
try {
    $pdo = new PDO(
        'mysql:host='.$config['db']['host'].';dbname='.$config['db']['name'],
        $config['db']['user'], $config['db']['pass'],
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    echo "✅ Database connected\n";
} catch (Exception $e) { die("❌ DB Error: ".$e->getMessage()."\n"); }

// Run migration
$sql = file_get_contents(__DIR__ . '/database/whatsapp_migration.sql');
$statements = array_filter(array_map('trim', explode(';', $sql)));
$created = 0;
foreach ($statements as $stmt) {
    if (empty($stmt) || strpos($stmt, '--') === 0) continue;
    try {
        $pdo->exec($stmt);
        if (stripos($stmt, 'CREATE TABLE') !== false) {
            preg_match('/CREATE TABLE IF NOT EXISTS `(\w+)`/', $stmt, $m);
            echo "✅ Table: " . ($m[1] ?? 'unknown') . "\n";
            $created++;
        }
    } catch (Exception $e) {
        echo "⚠️  " . substr($e->getMessage(), 0, 80) . "\n";
    }
}
echo "\n📋 Created/verified {$created} tables\n";

// Show all tables
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
echo "\n📊 Total tables: " . count($tables) . "\n";
echo "\n🎉 WhatsApp Suite installed!\n";
echo "⚠️  DELETE this file after setup!\n";
echo "</pre>";
?>
