-- Fix loan_type enum constraint issue
-- This script will update the loan_type enum to include 'personal' and other valid values

-- 1. First, let's check what the current enum values are
SELECT enumlabel FROM pg_enum WHERE enumtypid = (
  SELECT oid FROM pg_type WHERE typname = 'enum_loans_loan_type'
);

-- 2. Drop the existing enum constraint
ALTER TABLE loans DROP CONSTRAINT IF EXISTS loans_loan_type_check;

-- 3. Add the new enum constraint with all valid values
ALTER TABLE loans ADD CONSTRAINT loans_loan_type_check 
CHECK (loan_type IN ('personal', 'rental-property', 'home-improvement', 'investment-property', 'student-housing', 'business', 'vehicle', 'education', 'medical', 'debt-consolidation', 'other'));

-- 4. Update any existing records that might have invalid values
UPDATE loans SET loan_type = 'personal' WHERE loan_type NOT IN ('personal', 'rental-property', 'home-improvement', 'investment-property', 'student-housing', 'business', 'vehicle', 'education', 'medical', 'debt-consolidation', 'other');

-- 5. Verify the fix
SELECT loan_type, COUNT(*) FROM loans GROUP BY loan_type; 