-- Migration: Rename approval terminology to validation in database
--
-- Updates all user status values from:
-- - pending_approval → pending_validation
-- - pre_approved → pre_validated
--
-- This migration aligns with the client's request to change all "approval"
-- terminology to "validation" throughout the application.
--
-- IMPORTANT: This migration uses multiple transactions because PostgreSQL
-- requires enum values to be committed before they can be used.

-- ============================================================
-- TRANSACTION 1: Add new enum values
-- ============================================================

-- Add renamed values (approval → validation)
ALTER TYPE userstatus ADD VALUE IF NOT EXISTS 'pending_validation';
ALTER TYPE userstatus ADD VALUE IF NOT EXISTS 'pre_validated';

-- Add new status types from Phase 4 (if they don't already exist)
ALTER TYPE userstatus ADD VALUE IF NOT EXISTS 'canceled';
ALTER TYPE userstatus ADD VALUE IF NOT EXISTS 'expired';
ALTER TYPE userstatus ADD VALUE IF NOT EXISTS 'abandoned';

-- Commit the enum additions so they can be used
COMMIT;

-- Display progress
SELECT 'Step 1 completed: New enum values added' AS progress;

-- ============================================================
-- TRANSACTION 2: Update existing data
-- ============================================================

-- Start a new transaction
BEGIN;

-- Update pending_approval to pending_validation
UPDATE users
SET status = 'pending_validation'
WHERE status = 'pending_approval';

-- Update pre_approved to pre_validated
UPDATE users
SET status = 'pre_validated'
WHERE status = 'pre_approved';

-- Commit the data updates
COMMIT;

-- Success message
SELECT 'Migration completed: approval terminology updated to validation' AS result;

-- Note: All API endpoints and frontend components must also be updated
-- to use 'validation' terminology instead of 'approval'
--
-- Note: The old enum values 'pending_approval' and 'pre_approved' will remain
-- in the enum type but will not be used. This is normal PostgreSQL behavior.
