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]:
    """
    personnel_detail domain 的批量闭环回归。

    选 1 条真实存在的 personnel 作为 parent，构造 2 条新的 personnel_detail：

    1. round_1_create
       - 建立 personnel -> personnel_detail 绑定；
       - 覆盖 detail create 接口。
    2. round_2_update
       - 同时修改 2 条 detail 的 quantity / remark；
       - 覆盖按 personnel_id 分组的批量 update 接口。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/personnel_detail"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
        "contract_1.jsonl",
        "personnel_1.jsonl",
    ]
    local_personnel_id = "2006708a-dae4-4874-9207-22889cf2c886"
    local_detail_ids = [
        "1cda9548-ff40-431d-a012-fdb4dc8ce7f1",
        "b21b0365-6134-409a-9fa1-d0f6c1d22944",
    ]
    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="personnel_detail",
        round_name="round_1_create",
        purpose="首轮在同一 personnel 下创建 2 条明细，建立 parent/detail 绑定",
        required_bindings={
            "personnel": [local_personnel_id],
            "personnel_detail": local_detail_ids,
        },
        expected_bound_fields={
            "remote": {
                "personnel_detail": {
                    "1cda9548-ff40-431d-a012-fdb4dc8ce7f1": {
                        "date": "2024-06-15",
                        "quantity": 12,
                    },
                    "b21b0365-6134-409a-9fa1-d0f6c1d22944": {
                        "date": "2024-08-01",
                        "quantity": 18,
                    },
                }
            }
        },
        coverage={
            "api": ["POST /personnel/detail/create"],
            "notes": "在同一 personnel 下创建 2 条新 detail，覆盖明细 create 路径。",
        },
        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="personnel_detail",
        round_name="round_2_update",
        purpose="第二轮同时修改 2 条 personnel_detail，验证按 personnel_id 分组的批量 update",
        expected_changes={
            "personnel_detail": {
                "1cda9548-ff40-431d-a012-fdb4dc8ce7f1": ["quantity", "remark"],
                "b21b0365-6134-409a-9fa1-d0f6c1d22944": ["quantity", "remark"],
            }
        },
        required_bindings={
            "personnel": [local_personnel_id],
            "personnel_detail": local_detail_ids,
        },
        expected_bound_fields={
            "remote": {
                "personnel_detail": {
                    "1cda9548-ff40-431d-a012-fdb4dc8ce7f1": {
                        "quantity": 15,
                        "remark": "自动化更新-明细1",
                    },
                    "b21b0365-6134-409a-9fa1-d0f6c1d22944": {
                        "quantity": 22,
                        "remark": "自动化更新-明细2",
                    },
                }
            }
        },
        coverage={
            "api": ["PUT /personnel/detail/update"],
            "notes": "两条 detail 共用同一个 personnel_id，显式覆盖批量 detail update 接口。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="personnel_detail",
        description="人员明细 domain 的批量闭环测试。基于真实 personnel parent，先创建 2 条新 detail，再在第二轮同时更新两条明细，验证绑定与批量 update。",
        rounds=rounds,
    )
