<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\Migrations;

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

/**
 * Drops the two "read-only database user" settings.
 *
 * **Why.** The plugin used to ask the merchant to provision a second MySQL user with SELECT-only
 * grants, and told them on the Integrationen tab that `run_sql_read` was running on a weaker path
 * until they did. That is server setup we decided against (D31, code only) for a guarantee the
 * plugin now makes itself: {@see \Plugin\tnt_mcp\src\Mcp\Db\ReadGate} always opens its own
 * connection with the shop's own credentials, with multi-statements off, a session statement
 * timeout and `START TRANSACTION READ ONLY` — and refuses anything that is not a single SELECT at
 * the connection, not only at the parser.
 *
 * Removing the `<Setting>` entries from `info.xml` stops them being *offered*; the rows the
 * merchant already saved stay in `tplugineinstellungen` until deleted here. They may hold a
 * database password, so leaving them behind is not an option.
 *
 * Irreversible on purpose: `down()` cannot restore credentials it deleted, and nothing reads them
 * any more.
 */
class Migration20260805190000 extends Migration implements IMigration
{
    private const VALUE_NAMES = ['tnt_mcp_ro_db_user', 'tnt_mcp_ro_db_pass'];

    public function up(): void
    {
        foreach (self::VALUE_NAMES as $name) {
            $this->execute(
                "DELETE e FROM tplugineinstellungen e
                   INNER JOIN tplugin p ON p.kPlugin = e.kPlugin
                  WHERE p.cPluginID = 'tnt_mcp' AND e.cName = '" . $name . "'"
            );
            $this->execute(
                "DELETE c FROM tplugineinstellungenconf c
                   INNER JOIN tplugin p ON p.kPlugin = c.kPlugin
                  WHERE p.cPluginID = 'tnt_mcp' AND c.cWertName = '" . $name . "'"
            );
        }
    }

    public function down(): void
    {
        // Nothing to restore — the settings no longer exist in info.xml either.
    }
}
