from __future__ import annotations

import pytest

from sync_state_machine.domain.construction.api_handler import ConstructionTaskApiHandler
from sync_state_machine.domain.construction.sync_node import ConstructionTaskSyncNode


@pytest.mark.asyncio
async def test_construction_plan_update_uses_data_validation_and_reports_origin_status(monkeypatch: pytest.MonkeyPatch):
    handler = ConstructionTaskApiHandler()

    called = {"plan": 0, "base": 0}

    async def _fake_update_base(client, data, push_id: str, biz_id: str):
        called["base"] += 1

    async def _fake_update_plan(client, data: dict, push_id: str, biz_id: str):
        called["plan"] += 1
        assert data.get("plan_start_date") == "2025-11-28"
        assert data.get("plan_complete_date") == "2029-03-08"
        assert data.get("contract_quantity") == 196645.0

    monkeypatch.setattr(
        "sync_state_machine.domain.construction.api_handler.api_update_construction_task",
        _fake_update_base,
    )
    monkeypatch.setattr(
        "sync_state_machine.domain.construction.api_handler.api_update_construction_task_plan",
        _fake_update_plan,
    )

    handler.api_client = object()

    node = ConstructionTaskSyncNode(
        node_id="n1",
        data_id="e9be27e9-e4aa-4eaf-8255-1fe451737227",
        data={
            "id": "e9be27e9-e4aa-4eaf-8255-1fe451737227",
            "project_id": "f3e02e84-e81c-4aa6-88d4-f5aa108c8227",
            "contract_id": "663dfc72-d63c-42da-8853-4abcddd405f6",
            "show_name": "尾水隧洞开挖(方)",
            "construction_section": "TJ",
            "status": 1,
            "task_type": "wssdkw",
            "plan_start_date": "2025-11-28",
            "plan_complete_date": "2029-03-08",
            "actual_complete_date": None,
            "complete_quantity": 29930.0,
            "complete_rate": 15.22,
            "contract_quantity": 196645.0,
            "plan_node": [
                {"date": "2025-11-28", "quantity": 0.0, "is_audit": True},
                {"date": "2029-03-08", "quantity": 196645.0, "is_audit": True},
            ],
            "statistic_date": "2026-01-29",
            "is_crucial": False,
            "delay_days": 0,
        },
    )

    node.set_origin_data(
        {
            "id": "e9be27e9-e4aa-4eaf-8255-1fe451737227",
            "project_id": "f3e02e84-e81c-4aa6-88d4-f5aa108c8227",
            "contract_id": "663dfc72-d63c-42da-8853-4abcddd405f6",
            "show_name": "尾水隧洞开挖(方)",
            "construction_section": "TJ",
            "status": 1,
            "task_type": "wssdkw",
            "plan_start_date": None,
            "plan_complete_date": None,
            "actual_complete_date": "2026-01-29",
            "complete_quantity": 29930.0,
            "complete_rate": 0.0,
            "contract_quantity": None,
            "plan_node": [],
            "statistic_date": "2026-01-29",
            "is_crucial": False,
            "delay_days": None,
        }
    )

    results = await handler.update_all([node])

    assert len(results) == 1
    assert results[0].status.value == "success"
    assert called["plan"] == 1
    assert "UPDATE_SCHEMA: ConstructionPlanUpdate:" in (node.sync_log or "")
    assert "origin_invalid=" in (node.sync_log or "")
