<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\Migrations;

use JTL\Plugin\Migration;
use JTL\Update\IMigration;

/**
 * The change journal (design D29b, §4.2): one header row per `invoke_admin_action` that reached the
 * database, plus one row per affected database row with its before/after image.
 *
 * Two tables rather than one JSON blob, for two different readers: `undo_admin_action` iterates the
 * rows, and `list_recent_changes` summarises the headers without decoding megabytes of snapshots.
 *
 * Bounded like every other log this plugin writes — {@see \Plugin\tnt_mcp\src\Mcp\Journal\ChangeJournal}
 * prunes snapshots opportunistically (90 days / 200 MB / newest 200 entries always kept), so the
 * header history stays readable forever while the ability to undo expires.
 *
 * Idempotent (CREATE TABLE IF NOT EXISTS); no existing table is touched.
 */
class Migration20260804000000 extends Migration implements IMigration
{
    public function up(): void
    {
        $this->execute("CREATE TABLE IF NOT EXISTS `tnt_mcp_change` (
                `kChange` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                `cChangeId` VARCHAR(36) NOT NULL,
                `cActionId` VARCHAR(191) NOT NULL,
                `cTokenId` VARCHAR(191) NOT NULL DEFAULT '',
                `cActingLogin` VARCHAR(191) NOT NULL DEFAULT '',
                `cArgs` MEDIUMTEXT NOT NULL,
                `cVerdict` VARCHAR(32) NOT NULL DEFAULT 'incomplete',
                `cUndoState` VARCHAR(32) NOT NULL DEFAULT 'undoable',
                `cUndoReason` VARCHAR(500) NOT NULL DEFAULT '',
                `cCacheFlushed` VARCHAR(16) NOT NULL DEFAULT 'none',
                `cUndoOf` VARCHAR(36) NOT NULL DEFAULT '',
                `nSnapshotBytes` INT(10) UNSIGNED NOT NULL DEFAULT 0,
                `dCreated` DATETIME NOT NULL,
                `dUndone` DATETIME NULL,
                PRIMARY KEY (`kChange`),
                UNIQUE KEY `uq_tnt_mcp_change_id` (`cChangeId`),
                KEY `idx_tnt_mcp_change_created` (`dCreated`),
                KEY `idx_tnt_mcp_change_action` (`cActionId`, `dCreated`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

        $this->execute("CREATE TABLE IF NOT EXISTS `tnt_mcp_change_row` (
                `kChangeRow` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                `kChange` BIGINT(20) UNSIGNED NOT NULL,
                `cTable` VARCHAR(191) NOT NULL,
                `cKeyJson` VARCHAR(500) NOT NULL,
                `cOp` VARCHAR(8) NOT NULL,
                `cBefore` MEDIUMTEXT NULL,
                `cAfter` MEDIUMTEXT NULL,
                PRIMARY KEY (`kChangeRow`),
                KEY `idx_tnt_mcp_change_row` (`kChange`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    }

    public function down(): void
    {
        $this->execute('DROP TABLE IF EXISTS `tnt_mcp_change_row`');
        $this->execute('DROP TABLE IF EXISTS `tnt_mcp_change`');
    }
}
