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

CREATE TABLE IF NOT EXISTS activity_completions (
    id                  TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    user_id             TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    activity_id         TEXT NOT NULL,
    completed_at        TEXT NOT NULL DEFAULT (datetime('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              TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    completion_id   TEXT 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);
