-- Week 4: persist teacher recommendation actions (Assign / Accept / Ignore)

DO $$ BEGIN
  CREATE TYPE recommendation_status AS ENUM ('new', 'assigned', 'ignored');
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

CREATE TABLE IF NOT EXISTS teacher_recommendation_actions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  school_id UUID NOT NULL REFERENCES schools (id) ON DELETE CASCADE,
  teacher_id UUID NOT NULL REFERENCES teachers (id) ON DELETE CASCADE,
  -- class UUID or the literal 'all'
  class_scope TEXT NOT NULL DEFAULT 'all',
  recommendation_key TEXT NOT NULL,
  status recommendation_status NOT NULL DEFAULT 'new',
  assigned_count INTEGER,
  updated_by UUID REFERENCES users (id) ON DELETE SET NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  CONSTRAINT teacher_recommendation_actions_scope_key
    UNIQUE (teacher_id, class_scope, recommendation_key)
);

CREATE INDEX IF NOT EXISTS idx_teacher_rec_actions_teacher
  ON teacher_recommendation_actions (teacher_id, class_scope);

CREATE INDEX IF NOT EXISTS idx_teacher_rec_actions_school
  ON teacher_recommendation_actions (school_id);
