-- Week 3: XP ledger, objective mastery, avatar unlocks

DO $$ BEGIN
  CREATE TYPE mastery_status AS ENUM (
    'mastered', 'developing', 'not_yet_mastered'
  );
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

CREATE TABLE IF NOT EXISTS xp_events (
  id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  school_id           UUID NOT NULL REFERENCES schools (id) ON DELETE CASCADE,
  student_id          UUID NOT NULL REFERENCES students (id) ON DELETE CASCADE,
  activity_result_id  UUID REFERENCES activity_results (id) ON DELETE SET NULL,
  amount              INTEGER NOT NULL CHECK (amount > 0),
  reason              VARCHAR(80) NOT NULL,
  created_at          TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_xp_events_student ON xp_events (student_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_xp_events_school ON xp_events (school_id, created_at DESC);

CREATE TABLE IF NOT EXISTS objective_mastery (
  id                       UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  school_id                UUID NOT NULL REFERENCES schools (id) ON DELETE CASCADE,
  student_id               UUID NOT NULL REFERENCES students (id) ON DELETE CASCADE,
  objective_id             UUID NOT NULL REFERENCES learning_objectives (id) ON DELETE CASCADE,
  system_status            mastery_status NOT NULL DEFAULT 'not_yet_mastered',
  effective_status         mastery_status NOT NULL DEFAULT 'not_yet_mastered',
  teacher_override_status  mastery_status,
  overridden_by            UUID REFERENCES users (id) ON DELETE SET NULL,
  overridden_at            TIMESTAMPTZ,
  override_note            TEXT,
  attempts                 INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
  correct_count            INTEGER NOT NULL DEFAULT 0 CHECK (correct_count >= 0),
  last_result_at           TIMESTAMPTZ,
  created_at               TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at               TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  CONSTRAINT uq_objective_mastery_student_objective UNIQUE (student_id, objective_id)
);

CREATE INDEX IF NOT EXISTS idx_objective_mastery_school ON objective_mastery (school_id);
CREATE INDEX IF NOT EXISTS idx_objective_mastery_student ON objective_mastery (student_id);
CREATE INDEX IF NOT EXISTS idx_objective_mastery_objective ON objective_mastery (objective_id);

CREATE TABLE IF NOT EXISTS avatar_unlocks (
  id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  code        VARCHAR(40) NOT NULL UNIQUE,
  label       VARCHAR(120) NOT NULL,
  min_level   INTEGER NOT NULL CHECK (min_level >= 1),
  sort_order  INTEGER NOT NULL DEFAULT 0,
  is_active   BOOLEAN NOT NULL DEFAULT TRUE,
  created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS student_avatar_unlocks (
  id           UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  student_id   UUID NOT NULL REFERENCES students (id) ON DELETE CASCADE,
  unlock_id    UUID NOT NULL REFERENCES avatar_unlocks (id) ON DELETE CASCADE,
  unlocked_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  CONSTRAINT uq_student_avatar_unlock UNIQUE (student_id, unlock_id)
);

CREATE INDEX IF NOT EXISTS idx_student_avatar_unlocks_student
  ON student_avatar_unlocks (student_id);
