-- V010: ai_analysis_logs
-- AI 분석 실행 이력 테이블 (정확도 추적 포함)

CREATE TABLE ai_analysis_logs (
    id               BIGSERIAL PRIMARY KEY,
    group_id         BIGINT REFERENCES ai_agent_groups(id) ON DELETE SET NULL,
    agent_id         BIGINT REFERENCES ai_agents(id) ON DELETE SET NULL,
    schedule_id      BIGINT REFERENCES ai_agent_schedules(id) ON DELETE SET NULL,
    execution_type   VARCHAR(20) NOT NULL
                     CHECK (execution_type IN ('manual','api','schedule')),
    input_message    TEXT NOT NULL,
    analysis_result  TEXT NOT NULL,
    prediction       JSONB,
    actual_result    JSONB,
    accuracy_score   NUMERIC(5,2),
    tokens_used      INTEGER NOT NULL DEFAULT 0,
    duration_ms      INTEGER,
    metadata         JSONB NOT NULL DEFAULT '{}'::jsonb,
    company_code     VARCHAR(20),
    created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_analysis_group_created ON ai_analysis_logs (group_id, created_at DESC);
CREATE INDEX idx_analysis_company       ON ai_analysis_logs (company_code, created_at DESC);
