-- Activity events table for unified event logging
CREATE TYPE EVENT_TYPE AS ENUM (
    'grab', 'import', 'download_complete', 'download_failed',
    'quality_upgrade', 'safety_block', 'error', 'info'
);

CREATE TABLE activity_events (
    id            BIGSERIAL,
    event_type    EVENT_TYPE NOT NULL,
    media_id      BIGINT,
    media_type    MEDIA_TYPE,
    title         TEXT NOT NULL,
    description   TEXT,
    data          JSONB DEFAULT '{}',
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);

CREATE TABLE activity_events_current PARTITION OF activity_events
    FOR VALUES FROM (CURRENT_DATE - INTERVAL '30 days') TO (MAXVALUE);

CREATE INDEX idx_activity_type ON activity_events (event_type, created_at DESC);
CREATE INDEX idx_activity_media ON activity_events (media_id, media_type, created_at DESC) WHERE media_id IS NOT NULL;
CREATE INDEX idx_activity_created ON activity_events (created_at DESC);
