-- Migration: Absent (Pre-Final) — enums (access keys + template keys)
-- Description: Adds the PRE_FINAL absentee-communication variant for PT_TAT — the
--              "last but one" (penultimate) session. Until now the ABSENT purpose had only
--              REGULAR (all sessions except the last) and FINAL (last session) variants;
--              this introduces a distinct template for the session immediately before the
--              final one (e.g. the 11th of 12).
--
--              Kept in its own file, committed before the seed
--              (38_absent-pre-final-template-seed.sql) uses them — Postgres does not allow a
--              newly ADDed enum value to be used in the same transaction.
--
--   access key                              template_key                        purpose
--   --------------------------------------  ----------------------------------  ------------------
--   ABSENT_PRE_FINAL_SESSION_EMAIL_SEEKER   TAT_ABSENT_PRE_FINAL_EMAIL_SEEKER   Absent (pre-final)
--   ABSENT_PRE_FINAL_SESSION_WATI_SEEKER    TAT_ABSENT_PRE_FINAL_WATI_SEEKER    Absent (pre-final)
--
-- NOTE: application wiring is required for this occurrence to be selected at send time —
--       SessionOccurrence.PRE_FINAL, resolveOccurrence (penultimate-session detection) and the
--       SESSION_COMMUNICATION_TEMPLATES descriptor must be extended. See the seed file header.
-- Idempotent — safe to re-run.

-- ------------------------------------------------
-- communication_template_access_key_enum — the send-time access keys
-- ------------------------------------------------
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'ABSENT_PRE_FINAL_SESSION_EMAIL_SEEKER';
ALTER TYPE communication_template_access_key_enum ADD VALUE IF NOT EXISTS 'ABSENT_PRE_FINAL_SESSION_WATI_SEEKER';

-- ------------------------------------------------
-- template_key_enum — the master/clone template keys seeded in file 38 (PT_TAT)
-- ------------------------------------------------
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_ABSENT_PRE_FINAL_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'TAT_ABSENT_PRE_FINAL_WATI_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 (
    'ABSENT_PRE_FINAL_SESSION_EMAIL_SEEKER',
    'ABSENT_PRE_FINAL_SESSION_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_ABSENT_PRE_FINAL_EMAIL_SEEKER',
    'TAT_ABSENT_PRE_FINAL_WATI_SEEKER'
  )
ORDER BY enumlabel;
