<?php
namespace Tshen;

use PDO; use PDOException;

class Database {
    private static ?PDO $pdo = null;

    public static function init(string $path): void {
        $dir = dirname($path);
        if (!is_dir($dir)) { mkdir($dir, 0777, true); }
        try {
            self::$pdo = new PDO('sqlite:' . $path);
            self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            die('DB init error: ' . $e->getMessage());
        }
    }

    public static function pdo(): PDO { return self::$pdo; }

    public static function run(string $sql, array $params = []): \PDOStatement {
        $stmt = self::pdo()->prepare($sql);
        $stmt->execute($params);
        return $stmt;
    }
}