-- Model config: erstatter LiteLLM YAML.
-- Mapper alias (synops/low, synops/high) → provider + modell.
-- Fallback-kjede via priority.

CREATE TABLE IF NOT EXISTS model_config (
    id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    alias       TEXT NOT NULL,              -- "synops/low", "synops/high", etc.
    provider    TEXT NOT NULL,              -- "openrouter", "anthropic", "gemini", "xai", "openai", "ollama"
    model       TEXT NOT NULL,              -- "google/gemini-2.5-flash", "anthropic/claude-sonnet-4", etc.
    priority    SMALLINT NOT NULL DEFAULT 1, -- lavere = foretrukket. Fallback ved feil.
    is_active   BOOLEAN NOT NULL DEFAULT true,
    max_tokens  INTEGER DEFAULT 4096,
    description TEXT,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_model_config_alias ON model_config(alias, priority) WHERE is_active = true;

-- Seed: standard modell-konfigurasjon
INSERT INTO model_config (alias, provider, model, priority, description) VALUES
    ('synops/low',     'openrouter', 'google/gemini-2.5-flash',        1, 'Billig, rask — rutine, klassifisering'),
    ('synops/medium',  'openrouter', 'google/gemini-2.5-flash',        1, 'Mellomting — implementering, analyse'),
    ('synops/high',    'openrouter', 'anthropic/claude-sonnet-4',       1, 'Resonering, kreativitet, presisjon'),
    ('synops/high',    'openrouter', 'google/gemini-2.5-flash',        2, 'Fallback for synops/high'),
    ('synops/extreme', 'openrouter', 'anthropic/claude-opus-4',        1, 'Tung arkitektur og beslutninger')
ON CONFLICT DO NOTHING;

-- Modellpriser for kostnadsestimat i admin-UI
CREATE TABLE IF NOT EXISTS model_pricing (
    provider    TEXT NOT NULL,
    model       TEXT NOT NULL,
    input_price_per_m   NUMERIC,    -- pris per million input-tokens
    output_price_per_m  NUMERIC,    -- pris per million output-tokens
    updated_at  TIMESTAMPTZ DEFAULT now(),
    PRIMARY KEY (provider, model)
);

INSERT INTO model_pricing (provider, model, input_price_per_m, output_price_per_m) VALUES
    ('openrouter', 'google/gemini-2.5-flash',   0.15, 0.60),
    ('openrouter', 'anthropic/claude-sonnet-4',  3.00, 15.00),
    ('openrouter', 'anthropic/claude-opus-4',   15.00, 75.00)
ON CONFLICT DO NOTHING;
