-- =============================================================================
-- BeKind Backend — Add CCCD Document Types
-- Purpose: Extend document_type enum with Vietnamese ID (CCCD) variants
-- Depends on: 005-resident-and-facility-schema.sql (document_type enum)
-- =============================================================================

-- =============================================================================
-- MIGRATION: Add CCCD and Portrait Document Types
-- =============================================================================
-- Add three new document types to support Vietnamese ID (CCCD) and portrait photos:
-- - cccd_front: Căn Cước Công Dân mặt trước (Vietnamese ID - Front)
-- - cccd_back: Căn Cước Công Dân mặt sau (Vietnamese ID - Back)
-- - portrait: Ảnh chân dung (Portrait/Self photo)
--
-- Reasoning:
-- - id_card was too generic (could mean various types of IDs globally)
-- - CCCD front and back are separate documents (different data visible)
-- - Portrait photos are common requirement for resident verification
-- - Explicit naming improves clarity for business logic
--
-- NOTE: PostgreSQL enum values are immutable in type definition.
-- This migration adds new values to the existing enum type.
-- Order is preserved for documentation.
-- =============================================================================

-- Add CCCD front document type
ALTER TYPE document_type ADD VALUE 'cccd_front' AFTER 'id_card';

-- Add CCCD back document type
ALTER TYPE document_type ADD VALUE 'cccd_back' AFTER 'cccd_front';

-- Add portrait document type
ALTER TYPE document_type ADD VALUE 'portrait' AFTER 'cccd_back';

-- =============================================================================
-- Updated document_type Enum Values (in order)
-- =============================================================================
-- After this migration, valid document_type values are:
-- 1. id_card (legacy, can be deprecated)
-- 2. cccd_front (new)
-- 3. cccd_back (new)
-- 4. portrait (new)
-- 5. passport
-- 6. household_registration
-- 7. temporary_residence
-- 8. contract
-- 9. other
-- =============================================================================

-- =============================================================================
-- Verification Query (Run after migration)
-- =============================================================================
-- SELECT enumlabel, enumsortorder FROM pg_enum
-- WHERE enumtypid = (SELECT oid FROM pg_type WHERE typname = 'document_type')
-- ORDER BY enumsortorder;
--
-- Expected output:
-- enumlabel              | enumsortorder
-- ----------------------|---------------
-- id_card               | 1
-- cccd_front            | 2
-- cccd_back             | 3
-- portrait              | 4
-- passport              | 5
-- household_registration| 6
-- temporary_residence   | 7
-- contract              | 8
-- other                 | 9
-- =============================================================================

-- =============================================================================
-- Application Layer Updates Required
-- =============================================================================
-- After applying this migration:
--
-- 1. Update TypeScript Zod schema (src/contracts/resident/schema.ts):
--    const documentTypeSchema = z.enum([
--      'id_card',
--      'cccd_front',
--      'cccd_back',
--      'portrait',
--      'passport',
--      'household_registration',
--      'temporary_residence',
--      'contract',
--      'other'
--    ]);
--
-- 2. Regenerate models:
--    pnpm gen-db
--
-- 3. Update OpenAPI documentation:
--    - Values automatically included from Zod schema
--    - Examples: portrait, cccd_front, cccd_back
--
-- 4. Update frontend form (if hardcoded):
--    const DOCUMENT_TYPES = {
--      id_card: 'ID Card',
--      cccd_front: 'CCCD - Mặt Trước',
--      cccd_back: 'CCCD - Mặt Sau',
--      portrait: 'Ảnh Chân Dung',
--      passport: 'Hộ Chiếu',
--      household_registration: 'Hộ Khẩu',
--      temporary_residence: 'Tạm Trú',
--      contract: 'Hợp Đồng',
--      other: 'Khác'
--    };
-- =============================================================================

-- =============================================================================
-- Backward Compatibility
-- =============================================================================
-- ✅ Fully backward compatible:
-- - Existing document records with old types continue to work
-- - Old id_card type still valid (can migrate later if needed)
-- - New types only used for new documents
-- - No existing data modification required
-- - No schema changes needed
-- =============================================================================

-- =============================================================================
-- Rollback Instructions (If Needed)
-- =============================================================================
-- PostgreSQL does NOT support removing enum values directly.
-- Rollback is NOT possible without recreating the type.
--
-- If rollback is critical:
-- 1. Create new enum without the values
-- 2. Update all columns to use new enum type
-- 3. Drop old enum type
-- 4. Rename new enum type
--
-- This is complex and data-destructive. Only do if absolutely necessary.
-- Instead, just leave the new enum values unused if needed to revert.
-- =============================================================================
