<?php

declare(strict_types=1);

namespace System\Migration;

use PDO;
use System\Contracts\DatabaseInterface;

final class MigrationManager
{
    private PDO $db;

    public function __construct(DatabaseInterface $database)
    {
        $this->db = $database->getConnection();
    }

    public function initialize(): void
    {
        $this->db->exec("
            CREATE TABLE IF NOT EXISTS migrations (
                id INT AUTO_INCREMENT PRIMARY KEY,
                migration VARCHAR(255) NOT NULL UNIQUE,
                batch INT NOT NULL,
                executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ");
    }

    public function isInitialized(): bool
    {
        $statement = $this->db->query("SHOW TABLES LIKE 'migrations'");

        return $statement !== false && $statement->fetch() !== false;
    }
}