"""
Migrazione 0016: tabelle fascicoli e fascicolo_messages.
Fascicolazione / Dossier — raggruppa piu' comunicazioni PEC in una pratica.
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "0016"
down_revision = "0015"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ─── Tabella fascicoli ─────────────────────────────────────────────────────
    # L'ENUM fascicolo_stato viene creato automaticamente da SQLAlchemy
    # come parte di op.create_table (no op.execute separato)
    op.create_table(
        "fascicoli",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "tenant_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("tenants.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("titolo", sa.String(255), nullable=False),
        sa.Column("numero_pratica", sa.String(100), nullable=True),
        sa.Column(
            "stato",
            sa.Enum("aperto", "chiuso", "archiviato", name="fascicolo_stato"),
            nullable=False,
            server_default="aperto",
        ),
        sa.Column("categoria", sa.String(100), nullable=True),
        sa.Column(
            "responsabile_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column("scadenza", sa.DateTime(timezone=True), nullable=True),
        sa.Column("note", sa.Text, nullable=True),
        sa.Column(
            "created_by",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
    )
    op.create_index("idx_fascicoli_tenant", "fascicoli", ["tenant_id"])
    op.create_index("idx_fascicoli_stato", "fascicoli", ["stato"])
    op.create_index("idx_fascicoli_responsabile", "fascicoli", ["responsabile_id"])

    # ─── Tabella fascicolo_messages (N:M) ──────────────────────────────────────
    op.create_table(
        "fascicolo_messages",
        sa.Column(
            "fascicolo_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("fascicoli.id", ondelete="CASCADE"),
            primary_key=True,
        ),
        sa.Column(
            "message_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("messages.id", ondelete="CASCADE"),
            primary_key=True,
        ),
        sa.Column(
            "added_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "added_by",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
            nullable=True,
        ),
    )
    op.create_index(
        "idx_fascicolo_messages_fascicolo", "fascicolo_messages", ["fascicolo_id"]
    )
    op.create_index(
        "idx_fascicolo_messages_message", "fascicolo_messages", ["message_id"]
    )


def downgrade() -> None:
    op.drop_index("idx_fascicolo_messages_message", table_name="fascicolo_messages")
    op.drop_index("idx_fascicolo_messages_fascicolo", table_name="fascicolo_messages")
    op.drop_table("fascicolo_messages")

    op.drop_index("idx_fascicoli_responsabile", table_name="fascicoli")
    op.drop_index("idx_fascicoli_stato", table_name="fascicoli")
    op.drop_index("idx_fascicoli_tenant", table_name="fascicoli")
    op.drop_table("fascicoli")

    op.execute("DROP TYPE fascicolo_stato")
