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

    这一组用例沿用 filtered_datasource 中的批量 contract/material 数据，
    只聚焦 1 条目标物资，验证两个关键能力：

    1. round_1_create
       - 先建立 contract -> material 的绑定；
    2. round_2_update
         - 同一条 material 修改计划信息；
         - 覆盖 `MaterialPlanUpdate`。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/material"
    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",
    ]
    local_contract_id = "8cb77da2-1460-42b0-9ca4-7aa5afa58b99"
    local_material_id = "236c2610-f210-450a-846b-2241b2fd07f4"
    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",
        round_name="round_1_create",
        purpose="首轮建立 contract、material 的真实依赖链",
        required_bindings={
            "contract": [local_contract_id],
            "material": [local_material_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",
        round_name="round_2_update",
        purpose="第二轮修改物资计划节点，验证 plan update",
        expected_changes={
            "material": {
                local_material_id: [
                    "contract_quantity",
                    "plan_complete_date",
                    "plan_node",
                ]
            }
        },
        required_bindings={
            "contract": [local_contract_id],
            "material": [local_material_id],
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="material",
        description="物资 domain 的批量闭环测试。基于 filtered_datasource 中真实存在的批量 contract -> material 数据，先建立绑定，再在第二轮验证计划信息更新。",
        rounds=rounds,
    )
