from __future__ import annotations

from typing import Any

from auto_test_domains.base import build_domain_report, make_local_data_id, run_pipeline_round


REMOTE_PROJECT_ID = "f3e02e84-e81c-4aa6-88d4-f5aa108c8227"
REMOTE_UNITS_ID = "05bdc3b1-f455-4182-a50f-39ddfb3f5a69"
LOCAL_PROJECT_ID = make_local_data_id("project", REMOTE_PROJECT_ID)
LOCAL_UNITS_ID = make_local_data_id("units", REMOTE_UNITS_ID)


def run_case(config: dict[str, Any], report_root) -> dict[str, Any]:
    """
    units domain 回归。

    机组数据随项目自动生成，不支持 create/delete，因此这里只验证：

    1. round_1_create
       - 现有机组节点可以被正确加载并建立绑定。
    2. round_2_update
       - 修改 `actual_production_date`，确认 `PUT /units` 更新链路可用。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/units"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
    ]
    local_units_id = LOCAL_UNITS_ID
    project_id_remap = {
        "remote_project_id": REMOTE_PROJECT_ID,
        "local_project_id": LOCAL_PROJECT_ID,
        "remap_node_types": {"project", "units"},
    }

    rounds: list[dict[str, Any]] = []

    round_1 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_1_create",
        previous_dataset_dir=None,
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        domain_name="units",
        round_name="round_1_create",
        purpose="首轮建立机组基线绑定，确认项目自动生成的 units 节点可正常识别",
        required_bindings={
            "units": [local_units_id],
        },
        expected_bound_fields={
            "remote": {
                "units": {
                    local_units_id: {
                        "project_id": REMOTE_PROJECT_ID,
                        "serial_number": 1,
                    }
                }
            }
        },
        coverage={
            "notes": "确认 units 可从 project all_info.generator_units 中加载并建立绑定。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_1)

    round_2 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_2_update",
        previous_dataset_dir=f"{dataset_root}/round_1_create",
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        domain_name="units",
        round_name="round_2_update",
        purpose="第二轮回填机组实际投产日期，验证 units update 路径",
        expected_changes={
            "units": {
                local_units_id: ["actual_production_date"],
            }
        },
        required_bindings={
            "units": [local_units_id],
        },
        expected_bound_fields={
            "remote": {
                "units": {
                    local_units_id: {
                        "actual_production_date": "2025-12-20",
                    }
                }
            }
        },
        coverage={
            "api": ["PUT /units"],
            "notes": "显式验证 `actual_production_date` 会通过 units 更新接口推送到远端。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="units",
        description="机组 domain 的闭环测试。先确认自动生成节点可绑定，再验证 `PUT /units` 更新机组实际投产日期。",
        rounds=rounds,
    )
