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

    选用同一条真实存在的 contract_settlement 作为 parent，构造 2 条新的明细：

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

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/contract_settlement_detail"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
        "supplier_1.jsonl",
        "contract_1.jsonl",
        "contract_settlement_1.jsonl",
    ]
    local_settlement_id = "c4ded355-fb10-46e8-b829-442bf42c16f1"
    local_detail_ids = [
        "6ad2b1d0-1f86-46bb-9183-17d6c9882d31",
        "ddf5dfd7-c244-46a3-992a-fce9c4a42072",
    ]
    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="contract_settlement_detail",
        round_name="round_1_create",
        purpose="首轮在同一 settlement 下创建 2 条结算明细，建立 parent/detail 绑定",
        required_bindings={
            "contract_settlement": [local_settlement_id],
            "contract_settlement_detail": local_detail_ids,
        },
        expected_bound_fields={
            "remote": {
                "contract_settlement_detail": {
                    "6ad2b1d0-1f86-46bb-9183-17d6c9882d31": {
                        "date": "2025-10-19",
                        "quantity": 20000.0,
                    },
                    "ddf5dfd7-c244-46a3-992a-fce9c4a42072": {
                        "date": "2025-10-20",
                        "quantity": 26000.5,
                    },
                }
            }
        },
        coverage={
            "api": ["POST /contract_settlement/detail/create"],
            "notes": "在同一 settlement 下创建 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="contract_settlement_detail",
        round_name="round_2_update",
        purpose="第二轮同时修改 2 条结算明细，验证按 settlement_id 分组的批量 update",
        expected_changes={
            "contract_settlement_detail": {
                "6ad2b1d0-1f86-46bb-9183-17d6c9882d31": ["quantity", "remark"],
                "ddf5dfd7-c244-46a3-992a-fce9c4a42072": ["quantity", "remark"],
            }
        },
        required_bindings={
            "contract_settlement": [local_settlement_id],
            "contract_settlement_detail": local_detail_ids,
        },
        expected_bound_fields={
            "remote": {
                "contract_settlement_detail": {
                    "6ad2b1d0-1f86-46bb-9183-17d6c9882d31": {
                        "quantity": 21000.0,
                        "remark": "自动化更新-明细1",
                    },
                    "ddf5dfd7-c244-46a3-992a-fce9c4a42072": {
                        "quantity": 27500.75,
                        "remark": "自动化更新-明细2",
                    },
                }
            }
        },
        coverage={
            "api": ["PUT /contract_settlement/detail/update"],
            "notes": "两条 detail 共用同一个 settlement_id，显式覆盖批量 update 接口。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

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