<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\Migrations;

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

/**
 * Adds the request-log table the admin "Anfrage-Protokoll" tab reads. Every MCP HTTP request
 * (endpoint/tool, a non-sensitive params summary, auth mode, HTTP status, duration) is appended
 * here by {@see \Plugin\tnt_mcp\src\Mcp\Observability\RequestLogger} at the single dispatch choke
 * point. The log is bounded (the logger prunes to a fixed row cap) so it can never grow unbounded.
 * Idempotent (CREATE TABLE IF NOT EXISTS); the legacy tnt_mcp_token / tnt_mcp_audit and the OAuth
 * tables are untouched.
 */
class Migration20260724100000 extends Migration implements IMigration
{
    public function up(): void
    {
        $this->execute("CREATE TABLE IF NOT EXISTS `tnt_mcp_request_log` (
                `kLog` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                `cEndpoint` VARCHAR(191) NOT NULL DEFAULT '',
                `cParamsSummary` VARCHAR(500) NOT NULL DEFAULT '',
                `cAuthMode` VARCHAR(32) NOT NULL DEFAULT '',
                `nStatus` SMALLINT(5) UNSIGNED NOT NULL DEFAULT 0,
                `nDurationMs` INT(10) UNSIGNED NOT NULL DEFAULT 0,
                `dCreated` DATETIME NOT NULL,
                PRIMARY KEY (`kLog`),
                KEY `idx_tnt_mcp_request_log_created` (`dCreated`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    }

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