-- V2: Token-Erfassung mit Preis-Snapshot in processing_attempt;
-- neue model_price-Tabelle mit Composite Primary Key.
-- Verifizierter Stand: V1__initial_schema.sql ist die einzige bisherige
-- Migration im Projekt.

ALTER TABLE processing_attempt
    ADD COLUMN input_tokens INTEGER
    CHECK (input_tokens IS NULL OR (input_tokens >= 0 AND input_tokens <= 10000000));

ALTER TABLE processing_attempt
    ADD COLUMN output_tokens INTEGER
    CHECK (output_tokens IS NULL OR (output_tokens >= 0 AND output_tokens <= 10000000));

ALTER TABLE processing_attempt
    ADD COLUMN cache_creation_input_tokens INTEGER
    CHECK (cache_creation_input_tokens IS NULL OR (cache_creation_input_tokens >= 0 AND cache_creation_input_tokens <= 10000000));

ALTER TABLE processing_attempt
    ADD COLUMN cache_read_input_tokens INTEGER
    CHECK (cache_read_input_tokens IS NULL OR (cache_read_input_tokens >= 0 AND cache_read_input_tokens <= 10000000));

ALTER TABLE processing_attempt
    ADD COLUMN price_input_per_token_nano_usd INTEGER
    CHECK (price_input_per_token_nano_usd IS NULL OR (price_input_per_token_nano_usd >= 0 AND price_input_per_token_nano_usd <= 100000000));

ALTER TABLE processing_attempt
    ADD COLUMN price_output_per_token_nano_usd INTEGER
    CHECK (price_output_per_token_nano_usd IS NULL OR (price_output_per_token_nano_usd >= 0 AND price_output_per_token_nano_usd <= 100000000));

CREATE TABLE model_price (
    provider                          TEXT    NOT NULL,
    model_name                        TEXT    NOT NULL,
    price_input_per_token_nano_usd    INTEGER NOT NULL CHECK (price_input_per_token_nano_usd >= 0 AND price_input_per_token_nano_usd <= 100000000),
    price_output_per_token_nano_usd   INTEGER NOT NULL CHECK (price_output_per_token_nano_usd >= 0 AND price_output_per_token_nano_usd <= 100000000),
    currency                          TEXT    NOT NULL DEFAULT 'USD' CHECK (currency = 'USD'),
    updated_at                        TEXT    NOT NULL,
    PRIMARY KEY (provider, model_name)
);

CREATE INDEX idx_processing_attempt_started_at_provider_fp_model
    ON processing_attempt (started_at, ai_provider, fingerprint, model_name);

CREATE INDEX idx_processing_attempt_run_id_provider_model
    ON processing_attempt (run_id, ai_provider, model_name);

-- Default-Preise (Stand 2026-05-08, in Nano-USD pro Token)
-- Quellen (abgerufen 2026-05-08):
--   OpenAI:    https://openai.com/api/pricing/
--   Anthropic: https://www.anthropic.com/pricing
-- ON CONFLICT DO NOTHING: schuetzt vor manuell vorhandenen Default-Zeilen.
INSERT INTO model_price
    (provider, model_name, price_input_per_token_nano_usd, price_output_per_token_nano_usd, currency, updated_at)
VALUES
    ('openai-compatible', 'gpt-4o-mini',                  150,    600, 'USD', '2026-05-08T00:00:00Z'),
    ('openai-compatible', 'gpt-4o',                      2500,  10000, 'USD', '2026-05-08T00:00:00Z'),
    ('openai-compatible', 'gpt-4.1',                     2000,   8000, 'USD', '2026-05-08T00:00:00Z'),
    ('openai-compatible', 'gpt-4.1-mini',                 400,   1600, 'USD', '2026-05-08T00:00:00Z'),
    ('openai-compatible', 'gpt-4.1-nano',                 100,    400, 'USD', '2026-05-08T00:00:00Z'),
    ('openai-compatible', 'gpt-5',                       1250,  10000, 'USD', '2026-05-08T00:00:00Z'),
    ('openai-compatible', 'gpt-5-mini',                   250,   2000, 'USD', '2026-05-08T00:00:00Z'),
    ('claude',            'claude-haiku-4-5-20251001',   1000,   5000, 'USD', '2026-05-08T00:00:00Z'),
    ('claude',            'claude-sonnet-4-6',           3000,  15000, 'USD', '2026-05-08T00:00:00Z'),
    ('claude',            'claude-opus-4-7',             5000,  25000, 'USD', '2026-05-08T00:00:00Z')
ON CONFLICT (provider, model_name) DO NOTHING;
