from __future__ import annotations

from pydantic import BaseModel

from sync_state_machine.common.collection import DataCollection
from sync_state_machine.common.sync_node import SyncNode
from sync_state_machine.domain.project_detail.api_handler import ProjectDetailApiHandler


class _ProjectSchema(BaseModel):
    id: str


class _ProjectNode(SyncNode[_ProjectSchema]):
    node_type = "project"
    schema = _ProjectSchema


def test_project_detail_load_injects_project_id_before_validation() -> None:
    collection = DataCollection("remote")

    project = _ProjectNode(
        node_id="project-node-1",
        data_id="project-1",
        data={"id": "project-1"},
        context={
            "all_info": {
                "project_detail": [
                    {
                        "id": "detail-1",
                        "key": "plan_construction",
                        "value": [{"date": "2026-01-01", "capacity": 12.5}],
                    }
                ]
            }
        },
    )
    collection._nodes[project.node_id] = project

    handler = ProjectDetailApiHandler()
    handler.set_collection(collection)

    nodes = __import__("asyncio").run(handler.load())

    assert len(nodes) == 1
    detail_data = nodes[0].get_data()
    assert detail_data is not None
    assert detail_data["project_id"] == "project-1"
    assert detail_data["id"] == "detail-1"
