-- 001_tool_engine_config.sql
-- Target DB: dobetternorge_maindb (the tools' operational DB, via dbnmDb()).
-- Run manually after a mysqldump backup. Idempotent (IF NOT EXISTS).
--
-- DB-backed engine overrides for the legal tools. When a row exists and is
-- enabled, the matching resolver (dbnToolsResolveToolRun / dbnToolsReviewerModel)
-- uses its `engine` instead of the code/.env default. No row = unchanged behaviour.
--
-- tool_slug : a tool slug (ask|summarize|deep-research|korrespond|barnevernet|
--             discrepancy|legal-analysis...) OR '*' for an all-tools default.
-- scope     : tier_quick | tier_pro | legacy | persona_family | persona_general
-- engine    : tier/legacy scopes -> a ToolModels engine key (claude_haiku,
--             claude_sonnet, azure_mini, azure_full, gpu, nova_lite, regex);
--             persona scopes -> a reviewer model id (gpt-4o, gpt-4o-mini,
--             dbn-legal-agent-v3, dobetter-norge-v4, ...). Validated in code.

CREATE TABLE IF NOT EXISTS dbn_tool_engine_config (
    id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
    tool_slug   VARCHAR(64)  NOT NULL,
    scope       VARCHAR(32)  NOT NULL,
    engine      VARCHAR(64)  NOT NULL,
    enabled     TINYINT(1)   NOT NULL DEFAULT 1,
    updated_by  VARCHAR(190) DEFAULT NULL,
    updated_at  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_tool_scope (tool_slug, scope)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
