-- ============================================================
-- COMPREHENSIVE TEMPLATE ACCESS KEY FIX - MASTER SEED DATA
-- ============================================================
-- Date: 2026-04-07
-- Description: Complete fix for communication_templates_master including:
--   1. Add all missing enum values to communication_template_access_key_enum
--   2. Update master template_access_key values to recipient-specific variants
--   3. Fix merge field mappings for program_v1 and rm_name computed functions
--   4. Document missing PAYMENT_PENDING_REMINDER_WATI_RM template
--
-- SCOPE: This migration ONLY updates communication_templates_master (seed data)
--        Program-specific templates should be updated separately per program
-- ============================================================

-- ============================================================
-- PART 1: Add All Missing Enum Values
-- ============================================================
-- NOTE: PostgreSQL enum additions must be done outside transactions
-- These additions are idempotent and safe to run multiple times

-- Registration completed variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_WATI_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_SMS_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_SMS_RM';
    END IF;
END $$;

-- Registration cancelled variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_EMAIL_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_EMAIL_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_EMAIL_FINANCE' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_EMAIL_FINANCE';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_WATI_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_WATI_FINANCE' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_WATI_FINANCE';
    END IF;
END $$;

-- Refund variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_REFUND_EMAIL_FINANCE' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_REFUND_EMAIL_FINANCE';
    END IF;
END $$;

-- Blessed/Approval variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_NO_PAYMENT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_NO_PAYMENT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_WATI_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_SMS_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_SMS_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_NO_PAYMENT_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_NO_PAYMENT_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_NO_PAYMENT_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_NO_PAYMENT_WATI_RM';
    END IF;
END $$;

-- Hold variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'HOLD_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'HOLD_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'RM_HOLD_EMAIL_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'RM_HOLD_EMAIL_RM';
    END IF;
END $$;

-- Payment related variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'INVOICE_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'INVOICE_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'INVOICE_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'INVOICE_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PAYMENT_PENDING_REMINDER_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PAYMENT_PENDING_REMINDER_WATI_RM';
    END IF;
END $$;

-- Billing related variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BILLING_DETAILS_EDIT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BILLING_DETAILS_EDIT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BILLING_DETAILS_EDIT_REMOVED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BILLING_DETAILS_EDIT_REMOVED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'EINVOICE_ERROR_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'EINVOICE_ERROR_EMAIL_SEEKER';
    END IF;
END $$;

-- Swap related variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_DEMAND_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_DEMAND_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_ACCEPT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_ACCEPT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_ACCEPT_NO_PAYMENT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_ACCEPT_NO_PAYMENT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_DEMAND_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_DEMAND_WATI_SEEKER';
    END IF;
END $$;

-- Travel and coordinator related variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_TRAVEL_PLAN_CHANGE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_TRAVEL_PLAN_CHANGE_WATI';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_NEW_TRAVEL_PLAN_MADE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_NEW_TRAVEL_PLAN_MADE_WATI';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_RETURN_TRAVEL_PLAN_CHANGE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_RETURN_TRAVEL_PLAN_CHANGE_WATI';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_ONWARD_TRAVEL_PLAN_CHANGE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_ONWARD_TRAVEL_PLAN_CHANGE_WATI';
    END IF;
END $$;

-- Preference and admin variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PREFERENCE_EDITED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PREFERENCE_EDITED_WATI_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'ADMIN_MESSAGE_NOTIFICATION_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'ADMIN_MESSAGE_NOTIFICATION_WATI';
    END IF;
END $$;

-- Generic and OTP variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'GENERIC_EMAIL' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'GENERIC_EMAIL';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'OTP_EMAIL' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'OTP_EMAIL';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'OTP_SMS' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'OTP_SMS';
    END IF;
END $$;

-- Program specific variants
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'FIRST_TIMER_EMAIL' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'FIRST_TIMER_EMAIL';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CHECKLIST_EMAIL' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CHECKLIST_EMAIL';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PREPARATORY_EMAIL' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PREPARATORY_EMAIL';
    END IF;
END $$;


-- Add all missing enum values to communication_template_access_key_enum
-- This fixes errors like: invalid input value for enum communication_template_access_key_enum

-- NOTE: PostgreSQL enum additions must be done outside transactions
-- Run this script directly, not in a transaction block

-- Registration completed
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_COMPLETED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_COMPLETED_WATI_RM';
    END IF;
END $$;

-- Registration cancelled
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_EMAIL_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_EMAIL_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_EMAIL_FINANCE' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_EMAIL_FINANCE';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_WATI_RM';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_CANCELLED_WATI_FINANCE' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_CANCELLED_WATI_FINANCE';
    END IF;
END $$;

-- Refund
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'REGISTRATION_REFUND_EMAIL_FINANCE' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'REGISTRATION_REFUND_EMAIL_FINANCE';
    END IF;
END $$;

-- Blessed/Approval related
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_NO_PAYMENT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_NO_PAYMENT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_WATI_RM';
    END IF;
END $$;

-- CRITICAL: Add the missing ones that are causing the error
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_NO_PAYMENT_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_NO_PAYMENT_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BLESSED_NO_PAYMENT_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BLESSED_NO_PAYMENT_WATI_RM';
    END IF;
END $$;

-- Payment related
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'INVOICE_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'INVOICE_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'INVOICE_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'INVOICE_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PAYMENT_ACKNOWLEDGEMENT_OFFLINE_WATI_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PAYMENT_PENDING_REMINDER_WATI_RM' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PAYMENT_PENDING_REMINDER_WATI_RM';
    END IF;
END $$;

-- Billing related
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BILLING_DETAILS_EDIT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BILLING_DETAILS_EDIT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'BILLING_DETAILS_EDIT_REMOVED_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'BILLING_DETAILS_EDIT_REMOVED_EMAIL_SEEKER';
    END IF;
END $$;

-- Swap related
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_DEMAND_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_DEMAND_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_ACCEPT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_ACCEPT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_ACCEPT_NO_PAYMENT_EMAIL_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_ACCEPT_NO_PAYMENT_EMAIL_SEEKER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'SWAP_DEMAND_WATI_SEEKER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'SWAP_DEMAND_WATI_SEEKER';
    END IF;
END $$;

-- Travel and coordinator related
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_TRAVEL_PLAN_CHANGE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_TRAVEL_PLAN_CHANGE_WATI';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_NEW_TRAVEL_PLAN_MADE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_NEW_TRAVEL_PLAN_MADE_WATI';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_ONWARD_TRAVEL_PLAN_CHANGE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_ONWARD_TRAVEL_PLAN_CHANGE_WATI';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CO_ORDINATOR_RETURN_TRAVEL_PLAN_CHANGE_WATI' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CO_ORDINATOR_RETURN_TRAVEL_PLAN_CHANGE_WATI';
    END IF;
END $$;

-- Admin and notifications
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'ADMIN_MESSAGE_NOTIFICATION' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'ADMIN_MESSAGE_NOTIFICATION';
    END IF;
END $$;

-- Program specific notifications
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'FIRST_TIMER' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'FIRST_TIMER';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'CHECKLIST' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'CHECKLIST';
    END IF;
END $$;

DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'PREPARATORY' AND enumtypid = 'communication_template_access_key_enum'::regtype) THEN
        ALTER TYPE communication_template_access_key_enum ADD VALUE 'PREPARATORY';
    END IF;
END $$;

-- Verify additions
SELECT enumlabel 
FROM pg_enum 
WHERE enumtypid = 'communication_template_access_key_enum'::regtype
ORDER BY enumlabel;
