"""
Migrazione 0012: campi deadline su tabella messages
(Feature 4 – Scadenzario e tracking deadlines).
"""

from alembic import op
import sqlalchemy as sa

revision = "0012"
down_revision = "0011"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "messages",
        sa.Column("deadline_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        "messages",
        sa.Column("deadline_note", sa.Text, nullable=True),
    )
    op.create_index(
        "idx_messages_deadline",
        "messages",
        ["tenant_id", "deadline_at"],
        postgresql_where=sa.text("deadline_at IS NOT NULL"),
    )


def downgrade() -> None:
    op.drop_index("idx_messages_deadline", table_name="messages")
    op.drop_column("messages", "deadline_note")
    op.drop_column("messages", "deadline_at")
