"""Complète le suivi opérationnel des formations, stocks, contrats et compteurs.

Revision ID: d1e5f6a7b8c9
Revises: c0d4e5f6a7b8
"""
from alembic import op
import sqlalchemy as sa

revision = "d1e5f6a7b8c9"
down_revision = "c0d4e5f6a7b8"
branch_labels = None
depends_on = None


def upgrade():
    with op.batch_alter_table("trainings") as batch:
        batch.add_column(sa.Column("start_time", sa.Time(), nullable=True))
        batch.add_column(sa.Column("end_time", sa.Time(), nullable=True))
        batch.add_column(sa.Column("source_type", sa.String(30), nullable=True))
        batch.add_column(sa.Column("source_id", sa.String(191), nullable=True))
        batch.add_column(sa.Column("source_subject", sa.String(500), nullable=True))
        batch.add_column(sa.Column("source_sender", sa.String(255), nullable=True))
        batch.create_unique_constraint("uq_training_source", ["source_type", "source_id"])

    with op.batch_alter_table("meter_readings") as batch:
        batch.add_column(sa.Column("is_reset", sa.Boolean(), nullable=False, server_default=sa.false()))
        batch.add_column(sa.Column("reset_reason", sa.String(255), nullable=True))

    op.create_table(
        "part_stock_movements",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("part_id", sa.Integer(), sa.ForeignKey("parts.id", ondelete="CASCADE"), nullable=False),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
        sa.Column("movement_type", sa.String(20), nullable=False),
        sa.Column("quantity_delta", sa.Integer(), nullable=False),
        sa.Column("quantity_before", sa.Integer(), nullable=False),
        sa.Column("quantity_after", sa.Integer(), nullable=False),
        sa.Column("reason", sa.String(255), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
    )
    op.create_index("ix_part_stock_movements_part_id", "part_stock_movements", ["part_id"])

    op.create_table(
        "contract_visits",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("contract_id", sa.Integer(), sa.ForeignKey("contracts.id", ondelete="CASCADE"), nullable=False),
        sa.Column("scheduled_date", sa.Date(), nullable=False),
        sa.Column("status", sa.String(20), nullable=False, server_default="planifiee"),
        sa.Column("completed_at", sa.DateTime(), nullable=True),
        sa.Column("report", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.UniqueConstraint("contract_id", "scheduled_date", name="uq_contract_visit_date"),
    )
    op.create_index("ix_contract_visits_contract_id", "contract_visits", ["contract_id"])
    op.create_index("ix_contract_visits_scheduled_date", "contract_visits", ["scheduled_date"])


def downgrade():
    op.drop_table("contract_visits")
    op.drop_table("part_stock_movements")
    with op.batch_alter_table("meter_readings") as batch:
        batch.drop_column("reset_reason")
        batch.drop_column("is_reset")
    with op.batch_alter_table("trainings") as batch:
        batch.drop_constraint("uq_training_source", type_="unique")
        batch.drop_column("source_sender")
        batch.drop_column("source_subject")
        batch.drop_column("source_id")
        batch.drop_column("source_type")
        batch.drop_column("end_time")
        batch.drop_column("start_time")
