<?php

namespace App\Database;

use App\Core\Database;

/**
 * ITSWEBER CMS – Migration Runner
 *
 * Discovers and executes migration files in order.
 * Tracks executed migrations in iw_migrations table to avoid re-execution.
 * Falls back to idempotent full-run if tracking table does not exist.
 */
class Migrator
{
    private Database $db;
    private string $migrationsPath;

    public function __construct(Database $db, string $migrationsPath)
    {
        $this->db = $db;
        $this->migrationsPath = $migrationsPath;
    }

    /**
     * Run all pending migrations.
     * Creates the tracking table if it does not exist, then skips already-executed files.
     */
    public function runAll(): array
    {
        $this->ensureTrackingTable();

        $executed = $this->getExecuted();
        $files    = glob($this->migrationsPath . '/*.php');
        sort($files);

        $results = [];
        foreach ($files as $file) {
            $basename = basename($file);

            // Skip already-executed migrations
            if (in_array($basename, $executed, true)) {
                continue;
            }

            $className = $this->getClassName($file);
            require_once $file;

            $fullClass = 'App\\Database\\Migrations\\' . $className;
            if (!class_exists($fullClass)) {
                $fullClass = $className;
            }

            $migration = new $fullClass($this->db);
            $migration->up();

            $this->recordMigration($basename);
            $results[] = $basename;
        }

        return $results;
    }

    /**
     * Create the migrations tracking table if it does not exist.
     */
    private function ensureTrackingTable(): void
    {
        $this->db->query("
            CREATE TABLE IF NOT EXISTS iw_migrations (
                id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                migration VARCHAR(255) NOT NULL UNIQUE,
                executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
        ");
    }

    /**
     * Get list of already-executed migration filenames.
     */
    private function getExecuted(): array
    {
        try {
            $rows = $this->db->fetchAll("SELECT migration FROM iw_migrations ORDER BY id");
            return array_column($rows, 'migration');
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * Record a migration as executed.
     */
    private function recordMigration(string $filename): void
    {
        $this->db->query(
            "INSERT IGNORE INTO iw_migrations (migration) VALUES (:file)",
            ['file' => $filename]
        );
    }

    /**
     * Extract class name from migration file.
     */
    private function getClassName(string $file): string
    {
        $filename = pathinfo($file, PATHINFO_FILENAME);
        $name = preg_replace('/^\d+_/', '', $filename);
        return str_replace('_', '', ucwords($name, '_'));
    }
}
