CREATE TABLE IF NOT EXISTS ai_token_usage (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL,
    conversation_id UUID,
    model VARCHAR(128) NOT NULL,
    input_tokens INT NOT NULL,
    output_tokens INT NOT NULL,
    cost_usd NUMERIC(10, 6),
    recorded_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_ai_token_user ON ai_token_usage (user_id);
CREATE INDEX IF NOT EXISTS idx_ai_token_conv ON ai_token_usage (conversation_id);
CREATE INDEX IF NOT EXISTS idx_ai_token_recorded ON ai_token_usage (recorded_at);
