"""phase9 mail+notifications: 10 tables + person.email/role

Revision ID: p9c3d4e5f6a7
Revises: p8b2c3d4e5f6
Create Date: 2026-06-14 02:00:00.000000

"""
from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel
from alembic import op

revision: str = "p9c3d4e5f6a7"
down_revision: Union[str, Sequence[str], None] = "p8b2c3d4e5f6"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

S = sqlmodel.sql.sqltypes.AutoString


def upgrade() -> None:
    # person 확장
    with op.batch_alter_table("person") as b:
        b.add_column(sa.Column("email", S(), nullable=False, server_default=""))
        b.add_column(sa.Column("role", S(), nullable=False, server_default=""))

    op.create_table(
        "mail_account",
        sa.Column("id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("email", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("kind", S(), nullable=False),
        sa.Column("unread", sa.Integer(), nullable=False),
        sa.Column("connector", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "mail_folder",
        sa.Column("id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "email",
        sa.Column("id", S(), nullable=False),
        sa.Column("account", S(), nullable=False),
        sa.Column("from_key", S(), nullable=False),
        sa.Column("to", S(), nullable=False),
        sa.Column("subject", S(), nullable=False),
        sa.Column("time", S(), nullable=False),
        sa.Column("date", S(), nullable=False),
        sa.Column("read", sa.Boolean(), nullable=False),
        sa.Column("starred", sa.Boolean(), nullable=False),
        sa.Column("archived", sa.Boolean(), nullable=False),
        sa.Column("has_attach", sa.Boolean(), nullable=False),
        sa.Column("labels", sa.JSON(), nullable=True),
        sa.Column("preview", S(), nullable=False),
        sa.Column("body", sa.JSON(), nullable=True),
        sa.Column("attachments", sa.JSON(), nullable=True),
        sa.Column("ai_json", sa.JSON(), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["account"], ["mail_account.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "sent",
        sa.Column("id", S(), nullable=False),
        sa.Column("account", S(), nullable=False),
        sa.Column("to", S(), nullable=False),
        sa.Column("to_key", S(), nullable=False),
        sa.Column("subject", S(), nullable=False),
        sa.Column("time", S(), nullable=False),
        sa.Column("date", S(), nullable=False),
        sa.Column("preview", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["account"], ["mail_account.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "draft",
        sa.Column("id", S(), nullable=False),
        sa.Column("account", S(), nullable=False),
        sa.Column("to", S(), nullable=False),
        sa.Column("subject", S(), nullable=False),
        sa.Column("time", S(), nullable=False),
        sa.Column("preview", S(), nullable=False),
        sa.Column("body", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["account"], ["mail_account.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "notification",
        sa.Column("id", S(), nullable=False),
        sa.Column("bucket", S(), nullable=False),
        sa.Column("src", S(), nullable=False),
        sa.Column("app", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("sum", S(), nullable=False),
        sa.Column("time", S(), nullable=False),
        sa.Column("why", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("act", S(), nullable=False),
        sa.Column("held_by_rule", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "notification_guard",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("on", sa.Boolean(), nullable=False),
        sa.Column("until", S(), nullable=False),
        sa.Column("held", sa.Integer(), nullable=False),
        sa.Column("quiet", S(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "digest",
        sa.Column("id", S(), nullable=False),
        sa.Column("time", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("desc", S(), nullable=False),
        sa.Column("count", sa.Integer(), nullable=False),
        sa.Column("done", sa.Boolean(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "sender_rule",
        sa.Column("id", S(), nullable=False),
        sa.Column("who", S(), nullable=False),
        sa.Column("from_label", S(), nullable=False),
        sa.Column("rule", S(), nullable=False),
        sa.Column("bucket", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "notify_stats",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("total", sa.Integer(), nullable=False),
        sa.Column("seen", sa.Integer(), nullable=False),
        sa.Column("filtered", S(), nullable=False),
        sa.Column("later_more", sa.Integer(), nullable=False),
        sa.Column("held_more", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )


def downgrade() -> None:
    for t in (
        "notify_stats", "sender_rule", "digest", "notification_guard", "notification",
        "draft", "sent", "email", "mail_folder", "mail_account",
    ):
        op.drop_table(t)
    with op.batch_alter_table("person") as b:
        b.drop_column("role")
        b.drop_column("email")
