CREATE TABLE IF NOT EXISTS reporting_interaction_facts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    interaction_id TEXT NOT NULL,
    channel TEXT NULL,
    queue_id TEXT NULL,
    agent_id TEXT NULL,
    status TEXT NULL,
    created_at TEXT NULL,
    updated_at TEXT NOT NULL,
    closed_at TEXT NULL,
    answered INTEGER NULL,
    abandoned INTEGER NULL,
    wait_seconds INTEGER NULL,
    handle_seconds INTEGER NULL,
    resolved_first_contact INTEGER NULL,
    source TEXT NOT NULL DEFAULT 'unknown',
    CONSTRAINT uq_reporting_interaction_facts_interaction_id UNIQUE (interaction_id)
);

CREATE INDEX IF NOT EXISTS idx_reporting_interaction_facts_created_queue_channel
ON reporting_interaction_facts (created_at, queue_id, channel);

CREATE INDEX IF NOT EXISTS idx_reporting_interaction_facts_status_agent
ON reporting_interaction_facts (status, agent_id);
