-- Migration: 008 — Activity completions (charlas, talleres, presentaciones)
-- Database: PostgreSQL

CREATE TABLE IF NOT EXISTS activity_completions (
    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id             UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    activity_id         TEXT NOT NULL,
    completed_at        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    questionnaire_type  TEXT NOT NULL DEFAULT 'external',
    notes               TEXT,
    UNIQUE(user_id, activity_id)
);

CREATE INDEX IF NOT EXISTS idx_activity_completions_user       ON activity_completions(user_id);
CREATE INDEX IF NOT EXISTS idx_activity_completions_activity   ON activity_completions(activity_id);

CREATE TABLE IF NOT EXISTS activity_responses (
    id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    completion_id   UUID NOT NULL REFERENCES activity_completions(id) ON DELETE CASCADE,
    question_key    TEXT NOT NULL,
    answer          TEXT NOT NULL,
    UNIQUE(completion_id, question_key)
);

CREATE INDEX IF NOT EXISTS idx_activity_responses_completion ON activity_responses(completion_id);
