#!/usr/bin/env php
<?php
/**
 * ensure-db.php — Create database from schema.sql if it doesn't exist.
 *
 * Usage: php scripts/ensure-db.php [DB_PATH]
 * Default: storage/xamxam.db
 */

$root = dirname(__DIR__);
$dbPath = $argv[1] ?? ($root . '/storage/xamxam.db');
$schemaPath = $root . '/storage/schema.sql';

if (file_exists($dbPath)) {
    echo "Database already exists: $dbPath\n";
    exit(0);
}

if (!file_exists($schemaPath)) {
    die("Schema not found: $schemaPath\n");
}

$db = new PDO('sqlite:' . $dbPath);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = file_get_contents($schemaPath);
$db->exec($sql);

echo "Database created from schema: $dbPath\n";
