"""Phase 2.1b: executor, duration mode and explicit preventive scope.

All columns are nullable-safe/defaulted so existing installations keep their
behaviour.  The association table is additive and contains no copied files or
business records.
"""
from alembic import op
import sqlalchemy as sa

revision = "h3c4d5e6f7a8"
down_revision = "g2b3c4d5e6f7"
branch_labels = None
depends_on = None


def upgrade():
    for name in ("mowing_minutes", "edging_minutes", "hedge_minutes", "branch_removal_minutes"):
        op.add_column("zones", sa.Column(name, sa.Integer(), nullable=True))
    op.add_column("lot_tasks", sa.Column("duration_mode", sa.String(20), nullable=False, server_default="unit"))
    op.add_column("lot_tasks", sa.Column("executor_type", sa.String(30), nullable=False, server_default="undefined"))
    op.add_column("lot_tasks", sa.Column("scope_mode", sa.String(20), nullable=False, server_default="dynamic"))
    op.create_table(
        "lot_task_equipments",
        sa.Column("lot_task_id", sa.Integer(), nullable=False),
        sa.Column("equipment_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["lot_task_id"], ["lot_tasks.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["equipment_id"], ["equipments.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("lot_task_id", "equipment_id"),
    )


def downgrade():
    for name in ("branch_removal_minutes", "hedge_minutes", "edging_minutes", "mowing_minutes"):
        op.drop_column("zones", name)
    op.drop_table("lot_task_equipments")
    op.drop_column("lot_tasks", "scope_mode")
    op.drop_column("lot_tasks", "executor_type")
    op.drop_column("lot_tasks", "duration_mode")
