"""Aggiunge la tabella di associazione virtual_box_mailboxes

Revision ID: 0004
Revises: 0003
Create Date: 2026-03-19 00:00:00.000000

Aggiunge:
  - virtual_box_mailboxes  (associazione many-to-many tra virtual_boxes e mailboxes)
"""

from alembic import op

revision = "0004"
down_revision = "0003"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("""
        CREATE TABLE virtual_box_mailboxes (
            virtual_box_id  UUID NOT NULL REFERENCES virtual_boxes(id) ON DELETE CASCADE,
            mailbox_id      UUID NOT NULL REFERENCES mailboxes(id)     ON DELETE CASCADE,
            PRIMARY KEY (virtual_box_id, mailbox_id)
        )
    """)
    op.execute(
        "CREATE INDEX idx_vbox_mbox_vbox    ON virtual_box_mailboxes (virtual_box_id)"
    )
    op.execute(
        "CREATE INDEX idx_vbox_mbox_mailbox ON virtual_box_mailboxes (mailbox_id)"
    )


def downgrade() -> None:
    op.execute("DROP TABLE IF EXISTS virtual_box_mailboxes CASCADE")
