<?php

namespace OtomaticAi\Database;

use OtomaticAi\Database\Migrations\AddCustomInstructionsToPersonaTableMigration;
use OtomaticAi\Database\Migrations\AddGenerationStateColumnsMigration;
use OtomaticAi\Database\Migrations\AddLogsToPublicationTableMigration;
use OtomaticAi\Database\Migrations\CreateLinkingTableMigration;
use OtomaticAi\Database\Migrations\CreateNetlinkingTableMigration;
use OtomaticAi\Database\Migrations\CreateSingleModeGenerationsTableMigration;
use OtomaticAi\Database\Migrations\CreateTemplatesTableMigration;
use OtomaticAi\Database\Migrations\CreateThumbnailTasksTableMigration;
use OtomaticAi\Database\Migrations\InitialMigration;

abstract class Migrator
{
    static private $migrations = [
        "0.0.2" => [
            InitialMigration::class,
        ],
        "0.0.3" => [
            AddLogsToPublicationTableMigration::class,
        ],
        "0.0.4" => [
            CreateLinkingTableMigration::class,
        ],
        "0.0.5" => [
            CreateTemplatesTableMigration::class,
        ],
        "0.0.6" => [
            CreateSingleModeGenerationsTableMigration::class,
            AddCustomInstructionsToPersonaTableMigration::class,
        ],
        "0.0.7" => [
            CreateNetlinkingTableMigration::class,
        ],
        "0.0.8" => [
            CreateThumbnailTasksTableMigration::class,
        ],
        "0.0.9" => [
            AddGenerationStateColumnsMigration::class,
        ],
    ];

    static public function run()
    {
        $dbVersion = get_option(OTOMATIC_AI_DB_VERSION_OPTION, "0.0.0");
        $lastVersion = $dbVersion;
        foreach (self::$migrations as $version => $migrations) {
            foreach ($migrations as $migration) {
                $lastVersion = $version;
                $inst = new $migration($dbVersion, $version);
                $inst->run();
            }
        }

        if (version_compare($dbVersion, $lastVersion, "<")) {
            update_option(OTOMATIC_AI_DB_VERSION_OPTION, OTOMATIC_AI_DB_VERSION);
        }
    }
}
