-- ================================================
-- Migration: PAP-001 — PT_PAP email communication enums (access key, template keys, categories)
-- Date: 2026-08-08
--
-- Enum values needed by 56_pap-email-template-seed.sql. Kept in its own file so the values are
-- committed before the seed uses them — Postgres does not allow a newly ADDed enum value to be
-- used in the same transaction.
--
-- Idempotent — safe to re-run.
-- ================================================

-- ------------------------------------------------
-- communication_template_access_key_enum
--
-- Five of the six PAP emails reuse access keys that already exist and already have send sites in
-- the application, so they need no enum change:
--   1. Registration completion (submitted for approval) -> REGISTRATION_COMPLETED_EMAIL_SEEKER
--   2. Registration approved (seat allocated)           -> BLESSED_EMAIL_SEEKER
--   3. Online registration confirmed                    -> ONLINE_PAYMENT_ACKNOWLEDGEMENT_EMAIL_SEEKER
--   4. Invoice                                          -> INVOICE_EMAIL_SEEKER
--   5. Offline payment acknowledgement                  -> PAYMENT_ACKNOWLEDGEMENT_OFFLINE_EMAIL_SEEKER
--
-- Only the sixth is new. Offline payment realization previously notified the seeker over WhatsApp
-- only (OFFLINE_PAYMENT_COMPLETED_WATI_SEEKER); this adds the email counterpart, sent from
-- PaymentService.handlePaymentConfirmation on the non-online branch.
-- ------------------------------------------------
ALTER TYPE communication_template_access_key_enum
    ADD VALUE IF NOT EXISTS 'OFFLINE_PAYMENT_COMPLETED_EMAIL_SEEKER';

-- ------------------------------------------------
-- template_key_enum — the six master/clone template keys seeded in file 56
-- ------------------------------------------------
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'PAP_REGISTRATION_COMPLETED_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'PAP_BLESSED_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'PAP_ONLINE_PAYMENT_ACKNOWLEDGEMENT_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'PAP_INVOICE_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'PAP_PAYMENT_ACKNOWLEDGEMENT_OFFLINE_EMAIL_SEEKER';
ALTER TYPE template_key_enum ADD VALUE IF NOT EXISTS 'PAP_OFFLINE_PAYMENT_COMPLETED_EMAIL_SEEKER';

-- ------------------------------------------------
-- communication_category_enum — categories used by the six masters.
-- All six already exist (added by files 07 and 18); re-asserted here so file 56 can be run
-- against a database that has not had file 18 applied.
-- NOTE: 'online_payment_comfirmed' / 'offline_payment_comfirmed' carry a pre-existing typo in
-- the enum label itself (file 18). Spelled here exactly as stored — do not "fix" it in isolation.
-- ------------------------------------------------
ALTER TYPE communication_category_enum ADD VALUE IF NOT EXISTS 'registration_completed';
ALTER TYPE communication_category_enum ADD VALUE IF NOT EXISTS 'hdb_blessed';
ALTER TYPE communication_category_enum ADD VALUE IF NOT EXISTS 'online_payment_comfirmed';
ALTER TYPE communication_category_enum ADD VALUE IF NOT EXISTS 'invoice';
ALTER TYPE communication_category_enum ADD VALUE IF NOT EXISTS 'payment_acknowledgement';
ALTER TYPE communication_category_enum ADD VALUE IF NOT EXISTS 'offline_payment_comfirmed';

-- ------------------------------------------------
-- Verification
-- ------------------------------------------------
SELECT t.typname, e.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 = 'OFFLINE_PAYMENT_COMPLETED_EMAIL_SEEKER')
   OR (t.typname = 'template_key_enum' AND e.enumlabel LIKE 'PAP\_%')
ORDER BY t.typname, e.enumlabel;
