-- Migration Rollback: Remove multi-tenancy support from user service tables
-- Date: 2026-01-26
-- Description: Rollback tenant_id column from all user-related tables

-- ============================================================================
-- 4. Verification Codes table (if exists)
-- ============================================================================
DO $$
BEGIN
    IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'verification_codes') THEN
        DROP INDEX IF EXISTS idx_verification_codes_tenant;
        ALTER TABLE verification_codes DROP COLUMN IF EXISTS tenant_id;
    END IF;
END $$;

-- ============================================================================
-- 3. User Contacts table (if exists)
-- ============================================================================
DO $$
BEGIN
    IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_contacts') THEN
        DROP INDEX IF EXISTS idx_user_contacts_verified;
        DROP INDEX IF EXISTS idx_user_contacts_type;
        DROP INDEX IF EXISTS idx_user_contacts_user;
        DROP INDEX IF EXISTS idx_user_contacts_tenant;
        ALTER TABLE user_contacts DROP COLUMN IF EXISTS tenant_id;
    END IF;
END $$;

-- ============================================================================
-- 2. User Profiles table (if exists)
-- ============================================================================
DO $$
BEGIN
    IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_profiles') THEN
        DROP INDEX IF EXISTS idx_user_profiles_nationality;
        DROP INDEX IF EXISTS idx_user_profiles_user;
        DROP INDEX IF EXISTS idx_user_profiles_tenant;
        ALTER TABLE user_profiles DROP COLUMN IF EXISTS tenant_id;
    END IF;
END $$;

-- ============================================================================
-- 1. Users table
-- ============================================================================
DROP INDEX IF EXISTS idx_users_tenant_fingerprint;
DROP INDEX IF EXISTS idx_users_tenant_phone;
DROP INDEX IF EXISTS idx_users_tenant;
ALTER TABLE users DROP COLUMN IF EXISTS tenant_id;
