CREATE TABLE IF NOT EXISTS reporting_interaction_facts (
    id SERIAL PRIMARY KEY,
    interaction_id VARCHAR(64) NOT NULL,
    channel VARCHAR(32) NULL,
    queue_id VARCHAR(64) NULL,
    agent_id VARCHAR(128) NULL,
    status VARCHAR(32) NULL,
    created_at VARCHAR(64) NULL,
    updated_at VARCHAR(64) NOT NULL,
    closed_at VARCHAR(64) NULL,
    answered BOOLEAN NULL,
    abandoned BOOLEAN NULL,
    wait_seconds INTEGER NULL,
    handle_seconds INTEGER NULL,
    resolved_first_contact BOOLEAN NULL,
    source VARCHAR(64) 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);
