"""phase-21: 계정별 실제 폴더 미러(account_folder) + email.folder 라우팅

Revision ID: p21c9d0e1f2a3
Revises: p20b8c9d0e1f2
Create Date: 2026-06-19 00:00:00.000000

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "p21c9d0e1f2a3"
down_revision: Union[str, Sequence[str], None] = "p20b8c9d0e1f2"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "email",
        sa.Column("folder", sa.String(), nullable=False, server_default="inbox"),
    )
    op.add_column(
        "email",
        sa.Column("folder_name", sa.String(), nullable=False, server_default=""),
    )
    op.create_index("ix_email_folder", "email", ["folder"])
    op.create_table(
        "account_folder",
        sa.Column("id", sa.String(), primary_key=True),
        sa.Column("mail_account", sa.String(), sa.ForeignKey("mail_account.id"), index=True),
        sa.Column("slug", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("kind", sa.String(), nullable=False, server_default="system"),
        sa.Column("icon", sa.String(), nullable=False, server_default="folder"),
        sa.Column("provider_id", sa.String(), nullable=False, server_default=""),
        sa.Column("unread", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("total", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
    )


def downgrade() -> None:
    op.drop_table("account_folder")
    op.drop_index("ix_email_folder", table_name="email")
    op.drop_column("email", "folder_name")
    op.drop_column("email", "folder")
