CREATE TABLE IF NOT EXISTS gateway.fact_check_settings (
    singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
    provider_id uuid REFERENCES gateway.providers(id) ON DELETE SET NULL,
    model text NOT NULL DEFAULT '',
    timeout_seconds integer NOT NULL DEFAULT 15 CHECK (timeout_seconds BETWEEN 3 AND 60),
    updated_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
    updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
INSERT INTO gateway.fact_check_settings(singleton) VALUES(true) ON CONFLICT DO NOTHING;

CREATE TABLE IF NOT EXISTS gateway.fact_check_policies (
    id uuid PRIMARY KEY,
    scope text NOT NULL UNIQUE CHECK (scope='global' OR scope ~ '^(department|application|api_key):[0-9a-f-]{36}$'),
    enabled boolean NOT NULL DEFAULT false,
    mode text NOT NULL DEFAULT 'async' CHECK (mode IN ('async','sync')),
    action text NOT NULL DEFAULT 'observe' CHECK (action IN ('observe','annotate','block')),
    knowledge_base_ids uuid[] NOT NULL DEFAULT '{}',
    support_threshold integer NOT NULL DEFAULT 70 CHECK (support_threshold BETWEEN 0 AND 100),
    evidence_threshold double precision NOT NULL DEFAULT 0.35 CHECK (evidence_threshold BETWEEN 0 AND 1),
    top_k integer NOT NULL DEFAULT 4 CHECK (top_k BETWEEN 1 AND 10),
    max_claims integer NOT NULL DEFAULT 8 CHECK (max_claims BETWEEN 1 AND 20),
    revision bigint NOT NULL DEFAULT 1,
    created_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
    created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
    updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
    CHECK (mode='sync' OR action='observe'),
    CHECK (NOT enabled OR cardinality(knowledge_base_ids)>0)
);

CREATE TABLE IF NOT EXISTS gateway.fact_check_events (
    id uuid PRIMARY KEY,
    policy_id uuid REFERENCES gateway.fact_check_policies(id) ON DELETE SET NULL,
    request_id text NOT NULL,
    model text NOT NULL DEFAULT '',
    mode text NOT NULL CHECK (mode IN ('async','sync')),
    action text NOT NULL CHECK (action IN ('observe','annotate','block')),
    verdict text NOT NULL CHECK (verdict IN ('supported','unsupported','uncertain','error')),
    support_score integer CHECK (support_score BETWEEN 0 AND 100),
    latency_ms integer NOT NULL DEFAULT 0 CHECK (latency_ms >= 0),
    question text NOT NULL DEFAULT '' CHECK (octet_length(question)<=65536),
    answer text NOT NULL DEFAULT '' CHECK (octet_length(answer)<=65536),
    claims jsonb NOT NULL DEFAULT '[]'::jsonb CHECK (jsonb_typeof(claims)='array'),
    evidence jsonb NOT NULL DEFAULT '[]'::jsonb CHECK (jsonb_typeof(evidence)='array'),
    error text NOT NULL DEFAULT '' CHECK (length(error)<=4000),
    created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX IF NOT EXISTS fact_check_events_time_idx ON gateway.fact_check_events(created_at DESC);
CREATE INDEX IF NOT EXISTS fact_check_events_verdict_time_idx ON gateway.fact_check_events(verdict,created_at DESC);

COMMENT ON COLUMN gateway.fact_check_settings.provider_id IS
    'Reuses the encrypted Provider credential subsystem; fact-check settings never duplicate plaintext API keys or upstream URLs.';
