"""add email_accounts and permissions tables

Revision ID: 0002
Revises: fba4e3f40f05
Create Date: 2026-02-15 18:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '0002'
down_revision: Union[str, None] = '0001_5'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # 創建 email_accounts 表
    op.create_table(
        'email_accounts',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('tenant_id', sa.Integer(), nullable=False, comment='租戶 ID'),
        sa.Column('employee_id', sa.Integer(), nullable=False, comment='員工 ID'),
        sa.Column('email_address', sa.String(length=255), nullable=False, comment='郵件地址'),
        sa.Column('quota_mb', sa.Integer(), nullable=False, server_default='2048', comment='配額 (MB)'),
        sa.Column('forward_to', sa.String(length=255), nullable=True, comment='轉寄地址'),
        sa.Column('auto_reply', sa.Text(), nullable=True, comment='自動回覆內容'),
        sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true', comment='是否啟用'),
        sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
        sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('now()')),
        sa.PrimaryKeyConstraint('id'),
        sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ondelete='CASCADE'),
    )

    # 創建索引
    op.create_index('idx_email_accounts_email', 'email_accounts', ['email_address'], unique=True)
    op.create_index('idx_email_accounts_employee', 'email_accounts', ['employee_id'])
    op.create_index('idx_email_accounts_tenant', 'email_accounts', ['tenant_id'])
    op.create_index('idx_email_accounts_active', 'email_accounts', ['is_active'])

    # 創建 permissions 表
    op.create_table(
        'permissions',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('tenant_id', sa.Integer(), nullable=False, comment='租戶 ID'),
        sa.Column('employee_id', sa.Integer(), nullable=False, comment='員工 ID'),
        sa.Column('system_name', sa.String(length=100), nullable=False, comment='系統名稱'),
        sa.Column('access_level', sa.String(length=50), nullable=False, server_default='user', comment='存取層級'),
        sa.Column('granted_at', sa.DateTime(), nullable=False, server_default=sa.text('now()'), comment='授予時間'),
        sa.Column('granted_by', sa.Integer(), nullable=True, comment='授予人'),
        sa.PrimaryKeyConstraint('id'),
        sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['employee_id'], ['employees.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['granted_by'], ['employees.id'], ondelete='SET NULL'),
        sa.UniqueConstraint('employee_id', 'system_name', name='uq_employee_system'),
    )

    # 創建索引
    op.create_index('idx_permissions_employee', 'permissions', ['employee_id'])
    op.create_index('idx_permissions_tenant', 'permissions', ['tenant_id'])
    op.create_index('idx_permissions_system', 'permissions', ['system_name'])


def downgrade() -> None:
    # 刪除 permissions 表
    op.drop_index('idx_permissions_system', table_name='permissions')
    op.drop_index('idx_permissions_tenant', table_name='permissions')
    op.drop_index('idx_permissions_employee', table_name='permissions')
    op.drop_table('permissions')

    # 刪除 email_accounts 表
    op.drop_index('idx_email_accounts_active', table_name='email_accounts')
    op.drop_index('idx_email_accounts_tenant', table_name='email_accounts')
    op.drop_index('idx_email_accounts_employee', table_name='email_accounts')
    op.drop_index('idx_email_accounts_email', table_name='email_accounts')
    op.drop_table('email_accounts')
