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"
LOCAL_PROJECT_ID = make_local_data_id("project", REMOTE_PROJECT_ID)


def run_case(config: dict[str, Any], report_root) -> dict[str, Any]:
    """
    construction_task_detail domain 的批量闭环回归。

    detail 类更新走批量接口，因此这里显式选择同一个 construction_task 下的 2 条真实明细：

    1. round_1_create
       - 建立 construction_task -> construction_task_detail 绑定；
    2. round_2_update
       - 同时修改 2 条 detail 的数量与备注；
       - 验证批量 update 路径被真实覆盖。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/construction_task_detail"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
        "contract_1.jsonl",
        "contract_settlement_1.jsonl",
        "construction_task_1.jsonl",
    ]
    local_task_id = "cbb9c312-c8af-437a-a7a2-7f856c77f758"
    local_detail_ids = [
        "db98ec2d-1c6b-45f4-a176-7b560a8a89fe",
        "678ef40c-affa-4576-a7e2-f440d835f8a8",
    ]
    project_id_remap = {
        "remote_project_id": REMOTE_PROJECT_ID,
        "local_project_id": LOCAL_PROJECT_ID,
        "remap_node_types": {"project"},
    }

    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="construction_task_detail",
        round_name="round_1_create",
        purpose="首轮建立 construction_task 与 2 条 construction_task_detail 的真实依赖链",
        required_bindings={
            "construction_task": [local_task_id],
            "construction_task_detail": local_detail_ids,
        },
        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="construction_task_detail",
        round_name="round_2_update",
        purpose="第二轮同时修改同一施工任务下 2 条 detail，验证批量 update",
        expected_changes={
            "construction_task_detail": {
                "db98ec2d-1c6b-45f4-a176-7b560a8a89fe": ["quantity", "remark"],
                "678ef40c-affa-4576-a7e2-f440d835f8a8": ["quantity", "remark"],
            }
        },
        required_bindings={
            "construction_task": [local_task_id],
            "construction_task_detail": local_detail_ids,
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="construction_task_detail",
        description="施工任务明细 domain 的批量闭环测试。基于同一个 construction_task 下的 2 条真实 detail，先建立绑定，再在第二轮同时更新两条明细，覆盖批量 update 接口。",
        rounds=rounds,
    )
