<?php
$dbPath = __DIR__ . '/wedding_data.db';
$sqlFile = __DIR__ . '/init_sqlite.sql';

try {
    $pdo = new PDO("sqlite:$dbPath");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("PRAGMA foreign_keys = ON");
    
    $sql = file_get_contents($sqlFile);
    $statements = explode(';', $sql);
    
    $count = 0;
    foreach ($statements as $stmt) {
        $stmt = trim($stmt);
        if (!empty($stmt) && !preg_match('/^--/', $stmt)) {
            $pdo->exec($stmt);
            $count++;
        }
    }
    
    echo "✓ SQLite数据库初始化成功！\n";
    echo "✓ 共执行 $count 条SQL语句\n\n";
    
    $tables = $pdo->query("SELECT name FROM sqlite_master WHERE type='table'");
    echo "已创建的表：\n";
    while ($row = $tables->fetch(PDO::FETCH_ASSOC)) {
        echo "  - {$row['name']}\n";
    }
    
} catch (PDOException $e) {
    die("✗ 初始化失败: " . $e->getMessage() . "\n");
}
?>