"""Unifie les règles de maintenance dans les tâches de lot.

Revision ID: e6f0a1b2c3d4
Revises: d5e9f0a1b2c3
"""
from alembic import op
import sqlalchemy as sa

revision = "e6f0a1b2c3d4"
down_revision = "d5e9f0a1b2c3"
branch_labels = None
depends_on = None

def upgrade():
    for name, column in [
        ("trigger_type", sa.Column("trigger_type", sa.String(20), nullable=False, server_default="calendar")),
        ("trigger_event", sa.Column("trigger_event", sa.String(80))),
        ("meter_threshold", sa.Column("meter_threshold", sa.Float())),
        ("season_start_month", sa.Column("season_start_month", sa.Integer())),
        ("season_end_month", sa.Column("season_end_month", sa.Integer())),
        ("advance_days", sa.Column("advance_days", sa.Integer(), nullable=False, server_default="0")),
        ("is_active", sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true())),
    ]: op.add_column("lot_tasks", column)
    op.add_column("equipment_categories", sa.Column("recurrence_alert_enabled", sa.Boolean(), nullable=False, server_default=sa.false()))
    op.add_column("equipment_categories", sa.Column("recurrence_threshold", sa.Integer(), nullable=False, server_default="3"))
    op.add_column("equipment_categories", sa.Column("recurrence_window_days", sa.Integer(), nullable=False, server_default="365"))
    op.add_column("contracts", sa.Column("visit_interval_days", sa.Integer()))
    op.add_column("contracts", sa.Column("last_visit_date", sa.Date()))
    op.add_column("contracts", sa.Column("next_visit_date", sa.Date()))

def downgrade():
    for name in ("next_visit_date", "last_visit_date", "visit_interval_days"): op.drop_column("contracts", name)
    for name in ("recurrence_window_days", "recurrence_threshold", "recurrence_alert_enabled"): op.drop_column("equipment_categories", name)
    for name in ("is_active", "advance_days", "season_end_month", "season_start_month", "meter_threshold", "trigger_event", "trigger_type"): op.drop_column("lot_tasks", name)
