-- ============================================================================
-- Fix auth_otps foreign key constraint
-- ============================================================================

-- Drop the foreign key constraint since unconfirmed users might not be fully accessible
DO $$
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_constraint
    WHERE conname = 'auth_otps_user_id_fkey'
  ) THEN
    ALTER TABLE auth_otps DROP CONSTRAINT auth_otps_user_id_fkey;
  END IF;
END $$;

-- Add a check constraint to ensure user_id is a valid UUID
ALTER TABLE auth_otps ADD CONSTRAINT auth_otps_user_id_valid
  CHECK (user_id::text ~ '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'::text);

COMMENT ON TABLE auth_otps IS 'Stores OTP codes for email verification. No FK constraint to handle unconfirmed users.';

-- Return success message
DO $$
BEGIN
  RAISE NOTICE 'Foreign key constraint removed from auth_otps table';
END $$;
