-- V006: ai_agent_conversations
-- AI 에이전트 대화 세션 테이블

CREATE TABLE ai_agent_conversations (
    id               BIGSERIAL PRIMARY KEY,
    conversation_id  VARCHAR(64) NOT NULL UNIQUE,
    agent_id         BIGINT REFERENCES ai_agents(id) ON DELETE SET NULL,
    user_id          VARCHAR(64),
    api_key_id       BIGINT REFERENCES ai_agent_api_keys(id) ON DELETE SET NULL,
    title            VARCHAR(500),
    message_count    INTEGER NOT NULL DEFAULT 0,
    total_tokens     BIGINT NOT NULL DEFAULT 0,
    status           VARCHAR(20) NOT NULL DEFAULT 'active',
    metadata         JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_conv_agent   ON ai_agent_conversations (agent_id);
CREATE INDEX idx_conv_user    ON ai_agent_conversations (user_id);
CREATE INDEX idx_conv_apikey  ON ai_agent_conversations (api_key_id);
CREATE INDEX idx_conv_updated ON ai_agent_conversations (updated_at DESC);
