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)
CONTRACT_ID = "f3b9c510-568f-4bed-98a4-a602142e1957"
LOCAL_CONTRACT_CHANGE_ID = "d8f0a39c-0b4f-4f3e-9d91-8f6e7a11c001"


def run_case(config: dict[str, Any], report_root) -> dict[str, Any]:
    """
    contract_change domain 回归。

    基于当前主测试项目的真实 project/contract 基线，覆盖：

    1. round_1_create
       - `POST /contract_change/create`
    2. round_2_update
       - `PUT /contract_change/update`
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/contract_change"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "supplier_1.jsonl",
        "project_1.jsonl",
        "contract_1.jsonl",
    ]
    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_change",
        round_name="round_1_create",
        purpose="首轮创建合同变更，验证主测试项目上的 create_remote 路径",
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
            "contract": [CONTRACT_ID],
            "contract_change": [LOCAL_CONTRACT_CHANGE_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "contract_change": [LOCAL_CONTRACT_CHANGE_ID],
        },
        expected_bound_fields={
            "remote": {
                "contract_change": {
                    LOCAL_CONTRACT_CHANGE_ID: {
                        "project_id": REMOTE_PROJECT_ID,
                        "title": "自动化合同变更回归-创建",
                        "reason": "用于验证 contract_change create_remote 路径",
                        "after_amount": 9890.122299,
                        "change_time": "2025-11-14",
                    }
                }
            }
        },
        coverage={
            "api": ["POST /contract_change/create"],
            "notes": "直接依赖当前主测试项目与其既有合同，创建 1 条新的合同变更记录。",
        },
        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_change",
        round_name="round_2_update",
        purpose="第二轮更新合同变更标题/理由/金额/附件，验证 update 路径",
        expected_changes={
            "contract_change": {
                LOCAL_CONTRACT_CHANGE_ID: ["title", "reason", "after_amount", "attachment_ids"],
            }
        },
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
            "contract": [CONTRACT_ID],
            "contract_change": [LOCAL_CONTRACT_CHANGE_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "contract_change": [LOCAL_CONTRACT_CHANGE_ID],
        },
        expected_bound_fields={
            "remote": {
                "contract_change": {
                    LOCAL_CONTRACT_CHANGE_ID: {
                        "title": "自动化合同变更回归-更新",
                        "reason": "用于验证 contract_change update 路径",
                        "after_amount": 9895.422299,
                        "attachment_ids.0": "8e72f2aa198341bf98e1441087f5246d",
                        "attachment_ids.1": "dc6ce1933e204cd3b1e97f1eed0cfa71",
                    }
                }
            }
        },
        coverage={
            "api": ["PUT /contract_change/update"],
            "notes": "在已绑定记录上显式验证 update handler 能把标题、理由、金额与附件变更推到远端。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="contract_change",
        description="合同变更 domain 的闭环测试。基于主测试项目与既有合同，依次覆盖 create 与 update 两条接口。",
        rounds=rounds,
    )
