-- AI Guard Violations — records every message blocked by the content guard.
-- Migration: 20260815000001

CREATE TABLE IF NOT EXISTS ai_guard_violations (
    id           UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id      UUID REFERENCES users(id) ON DELETE SET NULL,
    message_excerpt TEXT NOT NULL,          -- first 200 chars, never the full text
    guard_type   VARCHAR(40) NOT NULL,      -- 'keyword' | 'length' | 'flood' | 'moderation_api'
    reason       TEXT NOT NULL,             -- human-readable rejection reason
    categories   JSONB,                     -- OpenAI moderation categories (moderation_api only)
    created_at   TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_ai_guard_violations_user_id    ON ai_guard_violations(user_id);
CREATE INDEX IF NOT EXISTS idx_ai_guard_violations_created_at ON ai_guard_violations(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ai_guard_violations_guard_type ON ai_guard_violations(guard_type);
