-- Migration: Session Communication — enums (access keys + template keys)
-- Description: All enum values used by the manually-triggered online-session communications
--              (Welcome / Absent / Invite / Value Card / Program completion) for PT_TAT.
--              Kept in one file so the values are committed before the seed
--              (28_session-communication-templates-seed.sql) uses them — Postgres does not
--              allow a newly ADDed enum value to be used in the same transaction.
--              Tracking reuses the existing hdb_communication_track table — a send's purpose
--              is derived from the template's access key — so no new table is created here.
-- Idempotent — safe to re-run.

-- ------------------------------------------------
-- communication_template_access_key_enum — the send-time access keys
-- ------------------------------------------------
-- Welcome — program-level, before the sessions start
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'WELCOME_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'WELCOME_WATI_SEEKER';
-- Absent — regular (all sessions except the last) and final (last session)
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'ABSENT_REGULAR_SESSION_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'ABSENT_REGULAR_SESSION_WATI_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'ABSENT_FINAL_SESSION_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'ABSENT_FINAL_SESSION_WATI_SEEKER';
-- Invite — regular (all sessions except the last) and final (last session)
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'INVITE_REGULAR_SESSION_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'INVITE_REGULAR_SESSION_WATI_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'INVITE_FINAL_SESSION_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'INVITE_FINAL_SESSION_WATI_SEEKER';
-- Value card — email only, occurrence-independent
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'VALUE_CARD_EMAIL_SEEKER';
-- Program completion — occurrence-independent
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'PROGRAM_COMPLETION_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'PROGRAM_COMPLETION_WATI_SEEKER';

-- ------------------------------------------------
-- template_key_enum — the master/clone template keys seeded in file 28 (PT_TAT)
-- ------------------------------------------------
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_WELCOME_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_INVITE_REGULAR_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_INVITE_FINAL_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_ABSENT_REGULAR_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_ABSENT_FINAL_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_PROGRAM_COMPLETION_EMAIL_SEEKER';
-- WhatsApp (WATI) template keys
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_WELCOME_WATI_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_INVITE_REGULAR_WATI_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_INVITE_FINAL_WATI_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_ABSENT_REGULAR_WATI_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_ABSENT_FINAL_WATI_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_PROGRAM_COMPLETION_WATI_SEEKER';
-- Value card (email only) — seeded separately in file 30
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_VALUE_CARD_EMAIL_SEEKER';

-- ------------------------------------------------
-- Verification
-- ------------------------------------------------
SELECT enumlabel
FROM pg_enum e
JOIN pg_type t ON t.oid = e.enumtypid
WHERE t.typname = 'communication_template_access_key_enum'
  AND e.enumlabel IN (
    'WELCOME_EMAIL_SEEKER', 'WELCOME_WATI_SEEKER',
    'ABSENT_REGULAR_SESSION_EMAIL_SEEKER', 'ABSENT_REGULAR_SESSION_WATI_SEEKER',
    'ABSENT_FINAL_SESSION_EMAIL_SEEKER', 'ABSENT_FINAL_SESSION_WATI_SEEKER',
    'INVITE_REGULAR_SESSION_EMAIL_SEEKER', 'INVITE_REGULAR_SESSION_WATI_SEEKER',
    'INVITE_FINAL_SESSION_EMAIL_SEEKER', 'INVITE_FINAL_SESSION_WATI_SEEKER',
    'VALUE_CARD_EMAIL_SEEKER',
    'PROGRAM_COMPLETION_EMAIL_SEEKER', 'PROGRAM_COMPLETION_WATI_SEEKER'
  )
ORDER BY enumlabel;

SELECT enumlabel
FROM pg_enum e
JOIN pg_type t ON t.oid = e.enumtypid
WHERE t.typname = 'template_key_enum'
  AND e.enumlabel IN (
    'TAT_WELCOME_EMAIL_SEEKER',
    'TAT_INVITE_REGULAR_EMAIL_SEEKER', 'TAT_INVITE_FINAL_EMAIL_SEEKER',
    'TAT_ABSENT_REGULAR_EMAIL_SEEKER', 'TAT_ABSENT_FINAL_EMAIL_SEEKER',
    'TAT_PROGRAM_COMPLETION_EMAIL_SEEKER',
    'TAT_WELCOME_WATI_SEEKER',
    'TAT_INVITE_REGULAR_WATI_SEEKER', 'TAT_INVITE_FINAL_WATI_SEEKER',
    'TAT_ABSENT_REGULAR_WATI_SEEKER', 'TAT_ABSENT_FINAL_WATI_SEEKER',
    'TAT_PROGRAM_COMPLETION_WATI_SEEKER'
  )
ORDER BY enumlabel;
