-- 008_ai_model_routing.sql — Modellruting for AI Gateway (LiteLLM).
--
-- Nye tabeller:
--   ai_model_aliases    — globale modellaliaser (sidelinja/rutine, sidelinja/resonering)
--   ai_model_providers  — leverandør-modeller med prioritert fallback per alias
--   ai_job_routing      — jobbtype → modellalias mapping
--
-- Ref: docs/infra/ai_gateway.md §3.2

-- =============================================================================
-- 1. Modellaliaser
-- =============================================================================

CREATE TABLE ai_model_aliases (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    alias         TEXT NOT NULL,
    description   TEXT,
    is_active     BOOLEAN NOT NULL DEFAULT true,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(alias)
);

COMMENT ON TABLE ai_model_aliases IS 'Globale modellaliaser — abstraherer bort leverandør-spesifikke modellnavn';

-- =============================================================================
-- 2. Leverandør-modeller med fallback
-- =============================================================================

CREATE TABLE ai_model_providers (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    alias_id      UUID NOT NULL REFERENCES ai_model_aliases(id) ON DELETE CASCADE,
    provider      TEXT NOT NULL,
    model         TEXT NOT NULL,
    api_key_env   TEXT NOT NULL,
    priority      SMALLINT NOT NULL,
    is_active     BOOLEAN NOT NULL DEFAULT true,
    UNIQUE(alias_id, model)
);

COMMENT ON TABLE ai_model_providers IS 'Leverandør-modeller per alias, sortert etter priority (lavere = prøves først)';

-- =============================================================================
-- 3. Jobbtype → modellalias mapping
-- =============================================================================

CREATE TABLE ai_job_routing (
    job_type      TEXT PRIMARY KEY,
    alias         TEXT NOT NULL,
    description   TEXT
);

COMMENT ON TABLE ai_job_routing IS 'Hvilken modellalias brukes for hvilken jobbtype';

-- =============================================================================
-- 4. Seed: initielle aliaser og providers
-- =============================================================================

-- Rutine-alias (billig, høyt volum)
INSERT INTO ai_model_aliases (alias, description) VALUES
    ('sidelinja/rutine', 'Billig, høyt volum — tekstvasking, research, metadata'),
    ('sidelinja/resonering', 'Høy kvalitet — resonneringsoppgaver, live-assistent');

-- Providers for sidelinja/rutine
INSERT INTO ai_model_providers (alias_id, provider, model, api_key_env, priority) VALUES
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/rutine'), 'xai', 'xai/grok-4-1-fast-non-reasoning', 'XAI_API_KEY', 1),
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/rutine'), 'gemini', 'gemini/gemini-2.5-flash-lite', 'GEMINI_API_KEY', 2),
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/rutine'), 'gemini', 'gemini/gemini-flash-lite-latest', 'GEMINI_API_KEY', 3),
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/rutine'), 'gemini', 'gemini/gemini-flash-latest', 'GEMINI_API_KEY', 4),
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/rutine'), 'openrouter', 'openrouter/google/gemini-2.5-flash', 'OPENROUTER_API_KEY', 5);

-- Providers for sidelinja/resonering
INSERT INTO ai_model_providers (alias_id, provider, model, api_key_env, priority) VALUES
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/resonering'), 'anthropic', 'anthropic/claude-sonnet-4-20250514', 'ANTHROPIC_API_KEY', 1),
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/resonering'), 'openrouter', 'openrouter/anthropic/claude-sonnet-4', 'OPENROUTER_API_KEY', 2),
    ((SELECT id FROM ai_model_aliases WHERE alias = 'sidelinja/resonering'), 'openrouter', 'openrouter/google/gemini-2.5-flash', 'OPENROUTER_API_KEY', 3);

-- Jobbtype → alias routing
INSERT INTO ai_job_routing (job_type, alias, description) VALUES
    ('ai_text_process', 'sidelinja/rutine', 'Tekstvasking og ✨-behandling, høyt volum'),
    ('whisper_postprocess', 'sidelinja/rutine', 'Transkripsjonsvasking etter Whisper'),
    ('research_clip', 'sidelinja/rutine', 'Research-oppsummering'),
    ('live_factoid_eval', 'sidelinja/resonering', 'Faktoid-vurdering under live sending — krever presisjon'),
    ('agent_respond', 'sidelinja/resonering', 'Claude chat-agent svar'),
    ('simple_prompt', 'sidelinja/rutine', 'Standard LLM-kall via synops-ai prompt');

-- =============================================================================
-- 5. Oppdater agent_identities config til ny alias
-- =============================================================================

UPDATE agent_identities
SET config = jsonb_set(config, '{model_alias}', '"sidelinja/resonering"')
WHERE agent_key = 'claude-main'
  AND config->>'model_alias' = 'resonering';

-- =============================================================================
-- 6. Tilganger
-- =============================================================================

GRANT SELECT ON ai_model_aliases TO synops_reader;
GRANT SELECT ON ai_model_providers TO synops_reader;
GRANT SELECT ON ai_job_routing TO synops_reader;
