<?php

declare(strict_types=1);

namespace App\Support;

use PDO;

final class Database
{
    private static string $path;
    private static ?PDO $connection = null;

    public static function configure(string $path): void
    {
        self::$path = $path;
    }

    public static function connection(): PDO
    {
        if (self::$connection instanceof PDO) {
            return self::$connection;
        }

        $directory = dirname(self::$path);
        if (!is_dir($directory)) {
            mkdir($directory, 0775, true);
        }

        self::$connection = new PDO('sqlite:' . self::$path);
        self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        self::$connection->exec('PRAGMA foreign_keys = ON');

        return self::$connection;
    }
}
