-- Migration: Add multi-tenancy support to user service tables
-- Date: 2026-01-26
-- Description: Add tenant_id column to all user-related tables for multi-tenancy support

-- ============================================================================
-- 1. Users table
-- ============================================================================
ALTER TABLE users
ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_users_tenant ON users(tenant_id);
CREATE INDEX IF NOT EXISTS idx_users_tenant_phone ON users(tenant_id, phone);
CREATE INDEX IF NOT EXISTS idx_users_tenant_fingerprint ON users(tenant_id, fingerprint);

COMMENT ON COLUMN users.tenant_id IS 'Tenant ID for multi-tenancy support';

-- ============================================================================
-- 2. User Profiles table (if exists)
-- ============================================================================
DO $$
BEGIN
    IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_profiles') THEN
        ALTER TABLE user_profiles
        ADD COLUMN IF NOT EXISTS tenant_id uuid;

        CREATE INDEX IF NOT EXISTS idx_user_profiles_tenant ON user_profiles(tenant_id);
        CREATE INDEX IF NOT EXISTS idx_user_profiles_user ON user_profiles(user_id);
        CREATE INDEX IF NOT EXISTS idx_user_profiles_nationality ON user_profiles(tenant_id, nationality);

        COMMENT ON COLUMN user_profiles.tenant_id IS 'Tenant ID for multi-tenancy support';
    END IF;
END $$;

-- ============================================================================
-- 3. User Contacts table (if exists)
-- ============================================================================
DO $$
BEGIN
    IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'user_contacts') THEN
        ALTER TABLE user_contacts
        ADD COLUMN IF NOT EXISTS tenant_id uuid;

        CREATE INDEX IF NOT EXISTS idx_user_contacts_tenant ON user_contacts(tenant_id);
        CREATE INDEX IF NOT EXISTS idx_user_contacts_user ON user_contacts(tenant_id, user_id);
        CREATE INDEX IF NOT EXISTS idx_user_contacts_type ON user_contacts(tenant_id, user_id, type);
        CREATE INDEX IF NOT EXISTS idx_user_contacts_verified ON user_contacts(tenant_id, is_verified);

        COMMENT ON COLUMN user_contacts.tenant_id IS 'Tenant ID for multi-tenancy support';
    END IF;
END $$;

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

        CREATE INDEX IF NOT EXISTS idx_verification_codes_tenant ON verification_codes(tenant_id);

        COMMENT ON COLUMN verification_codes.tenant_id IS 'Tenant ID for multi-tenancy support';
    END IF;
END $$;
