BEGIN;

CREATE UNIQUE INDEX IF NOT EXISTS uq_platform_tenants_code_active
    ON platform.tenants ((LOWER(code)))
    WHERE deleted_at IS NULL;

CREATE UNIQUE INDEX IF NOT EXISTS uq_platform_tenants_email_active
    ON platform.tenants ((LOWER(email)))
    WHERE deleted_at IS NULL
      AND email IS NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS uq_platform_tenants_support_email_active
    ON platform.tenants ((LOWER(support_email)))
    WHERE deleted_at IS NULL
      AND support_email IS NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS uq_platform_tenant_business_profiles_registration_active
    ON platform.tenant_business_profiles ((LOWER(registration_number)))
    WHERE deleted_at IS NULL
      AND registration_number IS NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS uq_platform_tenant_business_profiles_tax_identifier_active
    ON platform.tenant_business_profiles ((LOWER(tax_identifier)))
    WHERE deleted_at IS NULL
      AND tax_identifier IS NOT NULL;

COMMIT;
