-- Phase 3.5a: /ask 호출 관측 테이블
-- refusal rate 측정, 지표 3 분리 (full/partial/insufficient), defense layer 디버깅

CREATE TABLE IF NOT EXISTS ask_events (
    id BIGSERIAL PRIMARY KEY,
    query TEXT NOT NULL,
    user_id BIGINT REFERENCES users(id),
    completeness TEXT,               -- full / partial / insufficient
    synthesis_status TEXT,
    confidence TEXT,
    refused BOOLEAN DEFAULT false,
    classifier_verdict TEXT,          -- sufficient / insufficient / null (skipped)
    max_rerank_score REAL,
    aggregate_score REAL,
    hallucination_flags JSONB DEFAULT '[]',
    evidence_count INT,
    citation_count INT,
    defense_layers JSONB,            -- per-layer flag snapshot (score_gate, classifier, grounding)
    total_ms INT,
    created_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_ask_events_created ON ask_events(created_at);
CREATE INDEX IF NOT EXISTS idx_ask_events_completeness ON ask_events(completeness);
