"""
Migrazione 0015: tabelle signatures e signature_assignments.
Gestione firme automatiche per caselle PEC e Virtual Box.
"""

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

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


def upgrade() -> None:
    # ─── Tabella signatures ────────────────────────────────────────────────────
    op.create_table(
        "signatures",
        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("name", sa.String(255), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("body_html", sa.Text, nullable=True),
        sa.Column("body_text", 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(),
        ),
        sa.UniqueConstraint("tenant_id", "name", name="uq_signature_name_tenant"),
    )
    op.create_index("idx_signatures_tenant", "signatures", ["tenant_id"])

    # ─── Tabella signature_assignments ─────────────────────────────────────────
    op.create_table(
        "signature_assignments",
        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(
            "signature_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("signatures.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "mailbox_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("mailboxes.id", ondelete="CASCADE"),
            nullable=True,
        ),
        sa.Column(
            "virtual_box_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("virtual_boxes.id", ondelete="CASCADE"),
            nullable=True,
        ),
        sa.Column("context", sa.String(20), nullable=False, server_default="both"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.CheckConstraint(
            "(mailbox_id IS NOT NULL)::int + (virtual_box_id IS NOT NULL)::int = 1",
            name="ck_sig_assignment_target",
        ),
        sa.UniqueConstraint("mailbox_id", "context", name="uq_sig_mailbox_context"),
        sa.UniqueConstraint("virtual_box_id", "context", name="uq_sig_vbox_context"),
    )
    op.create_index("idx_sig_assign_tenant", "signature_assignments", ["tenant_id"])
    op.create_index("idx_sig_assign_mailbox", "signature_assignments", ["mailbox_id"])
    op.create_index("idx_sig_assign_vbox", "signature_assignments", ["virtual_box_id"])


def downgrade() -> None:
    op.drop_index("idx_sig_assign_vbox", table_name="signature_assignments")
    op.drop_index("idx_sig_assign_mailbox", table_name="signature_assignments")
    op.drop_index("idx_sig_assign_tenant", table_name="signature_assignments")
    op.drop_table("signature_assignments")

    op.drop_index("idx_signatures_tenant", table_name="signatures")
    op.drop_table("signatures")
