"""Relie le cycle de vie des équipements à la maintenance préventive.

Revision ID: a4b8c2d6e0f1
Revises: f1a2b3c4d5e6
"""
from alembic import op
import sqlalchemy as sa


revision = "a4b8c2d6e0f1"
down_revision = "f1a2b3c4d5e6"
branch_labels = None
depends_on = None


def upgrade():
    op.add_column("equipment_categories", sa.Column("default_management_mode", sa.String(30), nullable=False, server_default="quantitatif"))
    op.add_column("equipments", sa.Column("management_mode", sa.String(30)))
    op.add_column("equipment_quantity_movements", sa.Column("movement_type", sa.String(30), nullable=False, server_default="transfert"))

    op.add_column("lot_tasks", sa.Column("application_scope", sa.String(20), nullable=False, server_default="auto"))
    op.add_column("lot_tasks", sa.Column("unavailable_policy", sa.String(20), nullable=False, server_default="suspend"))
    op.add_column("lot_tasks", sa.Column("after_repair_policy", sa.String(20), nullable=False, server_default="preserve"))
    op.add_column("lot_tasks", sa.Column("is_safety_critical", sa.Boolean(), nullable=False, server_default=sa.false()))

    for name, column in [
        ("suspension_reason", sa.Column("suspension_reason", sa.String(500))),
        ("suspended_at", sa.Column("suspended_at", sa.DateTime())),
        ("room_name_snapshot", sa.Column("room_name_snapshot", sa.String(200))),
        ("lot_name_snapshot", sa.Column("lot_name_snapshot", sa.String(500))),
        ("equipment_name_snapshot", sa.Column("equipment_name_snapshot", sa.String(200))),
        ("lifecycle_status_snapshot", sa.Column("lifecycle_status_snapshot", sa.String(30))),
    ]:
        op.add_column("scheduled_tasks", column)

    op.create_table(
        "equipment_lifecycle_events",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("equipment_id", sa.Integer(), nullable=False),
        sa.Column("event_type", sa.String(40), nullable=False),
        sa.Column("previous_status", sa.String(30)),
        sa.Column("new_status", sa.String(30)),
        sa.Column("quantity", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("reason", sa.String(500)),
        sa.Column("room_id_snapshot", sa.Integer()),
        sa.Column("room_name_snapshot", sa.String(200)),
        sa.Column("lot_id_snapshot", sa.Integer()),
        sa.Column("lot_name_snapshot", sa.String(500)),
        sa.Column("intervention_id", sa.Integer()),
        sa.Column("created_by_id", sa.Integer()),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["equipment_id"], ["equipments.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["intervention_id"], ["interventions.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["created_by_id"], ["users.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_equipment_lifecycle_events_equipment_id", "equipment_lifecycle_events", ["equipment_id"])
    op.create_index("ix_equipment_lifecycle_events_event_type", "equipment_lifecycle_events", ["event_type"])
    op.create_index("ix_equipment_lifecycle_events_created_at", "equipment_lifecycle_events", ["created_at"])


def downgrade():
    op.drop_table("equipment_lifecycle_events")
    for name in ("lifecycle_status_snapshot", "equipment_name_snapshot", "lot_name_snapshot", "room_name_snapshot", "suspended_at", "suspension_reason"):
        op.drop_column("scheduled_tasks", name)
    for name in ("is_safety_critical", "after_repair_policy", "unavailable_policy", "application_scope"):
        op.drop_column("lot_tasks", name)
    op.drop_column("equipment_quantity_movements", "movement_type")
    op.drop_column("equipments", "management_mode")
    op.drop_column("equipment_categories", "default_management_mode")
