-- Stonks Oracle - Model Performance Metrics
-- Tracks extraction success/failure rates, latency, retries, confidence,
-- token usage estimates, and validation error distributions.
-- Requirements: 5.2, 5.4, 12.1, 12.2

CREATE TABLE model_performance_metrics (
    id                          UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    document_id                 UUID REFERENCES documents(id) ON DELETE SET NULL,
    ticker                      VARCHAR(20),
    model_name                  VARCHAR(200) NOT NULL,
    prompt_version              VARCHAR(100),
    schema_version              VARCHAR(50),
    success                     BOOLEAN NOT NULL,
    attempt_count               INTEGER NOT NULL DEFAULT 1,
    total_duration_ms           INTEGER NOT NULL DEFAULT 0,
    first_attempt_duration_ms   INTEGER DEFAULT 0,
    final_attempt_duration_ms   INTEGER DEFAULT 0,
    confidence                  FLOAT DEFAULT 0.0,
    validation_status           VARCHAR(50) NOT NULL DEFAULT 'unknown',
    validation_error_count      INTEGER DEFAULT 0,
    validation_warning_count    INTEGER DEFAULT 0,
    validation_errors           JSONB DEFAULT '[]',
    retry_count                 INTEGER DEFAULT 0,
    input_token_estimate        INTEGER DEFAULT 0,
    output_token_estimate       INTEGER DEFAULT 0,
    company_count               INTEGER DEFAULT 0,
    recorded_at                 TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Query by time range (dashboard primary access pattern)
CREATE INDEX idx_model_perf_recorded ON model_performance_metrics(recorded_at DESC);

-- Filter by model for per-model dashboards
CREATE INDEX idx_model_perf_model ON model_performance_metrics(model_name, recorded_at DESC);

-- Filter by success for failure analysis
CREATE INDEX idx_model_perf_success ON model_performance_metrics(success, recorded_at DESC);

-- Filter by validation status for schema failure dashboards
CREATE INDEX idx_model_perf_validation ON model_performance_metrics(validation_status);
