"""remove_deprecated_tenant_employees_table

Revision ID: 5e95bf5ff0af
Revises: 844ac73765a3
Create Date: 2026-02-23 01:07:49.244754

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '5e95bf5ff0af'
down_revision: Union[str, None] = '844ac73765a3'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # 警告：這個 migration 會刪除所有相關的測試資料
    # 確保在開發環境執行，不要在正式環境執行

    # Step 1: 刪除所有參照 tenant_employees 的外鍵約束
    op.drop_constraint('department_members_employee_id_fkey', 'tenant_dept_members', type_='foreignkey')
    op.drop_constraint('email_accounts_employee_id_fkey', 'tenant_email_accounts', type_='foreignkey')
    op.drop_constraint('network_drives_employee_id_fkey', 'tenant_network_drives', type_='foreignkey')
    op.drop_constraint('permissions_employee_id_fkey', 'tenant_permissions', type_='foreignkey')
    op.drop_constraint('permissions_granted_by_fkey', 'tenant_permissions', type_='foreignkey')

    # Step 2: 清空相關表的測試資料（因為外鍵已被移除）
    op.execute('TRUNCATE TABLE tenant_dept_members CASCADE')
    op.execute('TRUNCATE TABLE tenant_email_accounts CASCADE')
    op.execute('TRUNCATE TABLE tenant_network_drives CASCADE')
    op.execute('TRUNCATE TABLE tenant_permissions CASCADE')

    # Step 3: 刪除 tenant_employees 表
    op.drop_table('tenant_employees')


def downgrade() -> None:
    # 不支援 downgrade（無法復原已刪除的資料）
    raise NotImplementedError("Cannot downgrade from removing tenant_employees table")
