"""Add synchronization fields missing from older GMAO dumps.

Revision ID: e0f4a5b6c7d8
Revises: d9e3f4a5b6c7
Create Date: 2026-08-14
"""

from alembic import op
import sqlalchemy as sa


revision = "e0f4a5b6c7d8"
down_revision = "d9e3f4a5b6c7"
branch_labels = None
depends_on = None


def upgrade():
    op.add_column(
        "equipments",
        sa.Column("is_deleted", sa.Boolean(), nullable=False, server_default=sa.false()),
    )
    op.add_column(
        "equipments", sa.Column("deleted_at", sa.DateTime(), nullable=True)
    )
    op.add_column(
        "interventions",
        sa.Column("original_scheduled_date", sa.DateTime(), nullable=True),
    )
    op.add_column(
        "interventions", sa.Column("postpone_count", sa.Integer(), nullable=True)
    )
    op.add_column(
        "interventions",
        sa.Column("last_postpone_reason", sa.Text(), nullable=True),
    )
    op.add_column(
        "interventions", sa.Column("last_postpone_at", sa.DateTime(), nullable=True)
    )
    op.add_column(
        "interventions",
        sa.Column("preventive_task_id", sa.Integer(), nullable=True),
    )
    op.add_column(
        "interventions", sa.Column("lot_task_id", sa.Integer(), nullable=True)
    )
    op.add_column(
        "interventions", sa.Column("postpone_reason", sa.Text(), nullable=True)
    )
    op.add_column(
        "gmao_context",
        sa.Column("outlook_sync_enabled", sa.Boolean(), nullable=True),
    )
    op.add_column(
        "gmao_context",
        sa.Column("outlook_sync_interval", sa.Integer(), nullable=True),
    )
    op.add_column(
        "gmao_context",
        sa.Column("outlook_sync_lookback_hours", sa.Integer(), nullable=True),
    )
    op.add_column(
        "gmao_context",
        sa.Column("ai_provider", sa.String(length=20), nullable=True),
    )
    op.add_column(
        "gmao_context",
        sa.Column("ollama_model", sa.String(length=100), nullable=True),
    )
    op.add_column(
        "gmao_context",
        sa.Column("watchdog_enabled", sa.Boolean(), nullable=True),
    )
    op.add_column(
        "gmao_context",
        sa.Column("watchdog_interval", sa.Integer(), nullable=True),
    )

    op.execute(
        "UPDATE gmao_context SET "
        "outlook_sync_enabled = COALESCE(outlook_sync_enabled, TRUE), "
        "outlook_sync_interval = COALESCE(outlook_sync_interval, 10), "
        "outlook_sync_lookback_hours = COALESCE(outlook_sync_lookback_hours, 24), "
        "ai_provider = COALESCE(ai_provider, 'openrouter'), "
        "watchdog_enabled = COALESCE(watchdog_enabled, FALSE), "
        "watchdog_interval = COALESCE(watchdog_interval, 10)"
    )


def downgrade():
    op.drop_column("gmao_context", "watchdog_interval")
    op.drop_column("gmao_context", "watchdog_enabled")
    op.drop_column("gmao_context", "ollama_model")
    op.drop_column("gmao_context", "ai_provider")
    op.drop_column("gmao_context", "outlook_sync_lookback_hours")
    op.drop_column("gmao_context", "outlook_sync_interval")
    op.drop_column("gmao_context", "outlook_sync_enabled")
    op.drop_column("interventions", "postpone_reason")
    op.drop_column("interventions", "lot_task_id")
    op.drop_column("interventions", "preventive_task_id")
    op.drop_column("interventions", "last_postpone_at")
    op.drop_column("interventions", "last_postpone_reason")
    op.drop_column("interventions", "postpone_count")
    op.drop_column("interventions", "original_scheduled_date")
    op.drop_column("equipments", "deleted_at")
    op.drop_column("equipments", "is_deleted")
