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

    这里仍按真实依赖来组织数据，并以 filtered_datasource 中的批量
    supplier -> contract -> settlement 数据作为基础集：

    - settlement 依赖 `project_id`
    - settlement 依赖 `contract_id`
        - contract 又依赖 supplier
        - 第二轮 settlement 的计划更新，会同步影响同一条 contract 的
            `sign_date` 与 `total_price`

    所以这里不用“缺供应商的空合同”，而是直接选一组
    filtered_datasource 中真实存在、关系完整的 supplier -> contract -> settlement。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/contract_settlement"
    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_supplier_id = "e88c8c18-bc49-4bab-acc5-6fb78f4c7f8f"
    local_contract_id = "eba6d9e0-fad4-456d-a325-38f5bd064b33"
    local_settlement_id = "c4ded355-fb10-46e8-b829-442bf42c16f1"
    project_id_remap = {
        "remote_project_id": REMOTE_PROJECT_ID,
        "local_project_id": LOCAL_PROJECT_ID,
        "remap_node_types": {"project"},
    }

    rounds: list[dict[str, Any]] = []

    # 用例 1：用真实依赖链先建立 supplier / contract / settlement 的绑定关系。
    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",
        round_name="round_1_create",
        purpose="首轮建立 supplier、contract、contract_settlement 的真实依赖链",
        required_bindings={
            "supplier": [local_supplier_id],
            "contract": [local_contract_id],
            "contract_settlement": [local_settlement_id],
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_1)

    # 用例 2：这条 rejected settlement 允许再次修改计划。
    # 本轮同时更新 settlement 与其对应 contract 的关键字段，保持两边一致。
    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",
        round_name="round_2_update",
        purpose="第二轮更新结算计划，并同步更新关联合同金额和签订日期",
        expected_changes={
            "contract_settlement": {
                local_settlement_id: [
                    "contract_price",
                    "plan_start_date",
                    "plan_complete_date",
                    "plan_node",
                ]
            }
        },
        required_bindings={
            "supplier": [local_supplier_id],
            "contract": [local_contract_id],
            "contract_settlement": [local_settlement_id],
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="contract_settlement",
        description="合同结算 domain 的批量闭环测试。基于 filtered_datasource 中真实存在的批量 supplier -> contract -> settlement 数据，验证 settlement 计划更新时不会破坏上游依赖，并保持批量场景下的状态库与绑定正常。",
        rounds=rounds,
    )