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

    选 1 条真实存在的物资明细，覆盖：

    1. round_1_create
       - 建立 material -> material_detail 绑定；
    2. round_2_update
       - 修改明细数量与备注；
       - 同时校验 parent material 的聚合字段变化不会破坏闭环。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/material_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",
        "material_1.jsonl",
        "material_detail_1.jsonl",
    ]
    local_material_id = "506a1188-0c20-44fc-8dba-1a36a074bfea"
    local_detail_id = "e996f4ef-3cff-476c-9b1b-a3d940ac24d6"
    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="material_detail",
        round_name="round_1_create",
        purpose="首轮建立 material、material_detail 的真实依赖链",
        required_bindings={
            "material": [local_material_id],
            "material_detail": [local_detail_id],
        },
        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="material_detail",
        round_name="round_2_update",
        purpose="第二轮修改物资明细数量与备注，验证 detail update",
        expected_changes={
            "material_detail": {
                local_detail_id: [
                    "quantity",
                    "remark",
                ]
            }
        },
        required_bindings={
            "material": [local_material_id],
            "material_detail": [local_detail_id],
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="material_detail",
        description="物资明细 domain 的批量闭环测试。基于 filtered_datasource 中真实存在的批量 material -> material_detail 数据，先建立绑定，再验证明细更新及 parent material 聚合结果。",
        rounds=rounds,
    )
