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)
LOCAL_LAR_CHANGE_ID = "a78be5df-c3a0-4bd7-8f31-9bdc2f22c001"


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

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

    1. round_1_create
       - `POST /lar/change`
    2. round_2_update
       - `PUT /lar/change`
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/lar_change"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_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="lar_change",
        round_name="round_1_create",
        purpose="首轮创建移民变更，验证 lar_change create_remote 路径",
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
            "lar_change": [LOCAL_LAR_CHANGE_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "lar_change": [LOCAL_LAR_CHANGE_ID],
        },
        expected_bound_fields={
            "remote": {
                "lar_change": {
                    LOCAL_LAR_CHANGE_ID: {
                        "project_id": REMOTE_PROJECT_ID,
                        "title": "自动化移民变更回归-创建",
                        "reason": "用于验证 lar_change create_remote 路径",
                        "after_amount": 1.5,
                        "change_time": "2025-11-12",
                    }
                }
            }
        },
        coverage={
            "api": ["POST /lar/change"],
            "notes": "在当前主测试项目上创建 1 条新的移民变更记录，确认 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="lar_change",
        round_name="round_2_update",
        purpose="第二轮更新移民变更标题/理由/金额/附件，验证 update 路径",
        expected_changes={
            "lar_change": {
                LOCAL_LAR_CHANGE_ID: ["title", "reason", "after_amount", "attachment_ids"],
            }
        },
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
            "lar_change": [LOCAL_LAR_CHANGE_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "lar_change": [LOCAL_LAR_CHANGE_ID],
        },
        expected_bound_fields={
            "remote": {
                "lar_change": {
                    LOCAL_LAR_CHANGE_ID: {
                        "title": "自动化移民变更回归-更新",
                        "reason": "用于验证 lar_change update 路径",
                        "after_amount": 2.5,
                        "attachment_ids.0": "3f5f674fd36c413aa121403d2199f02a",
                        "attachment_ids.1": "fb6e840376594c13a527ab10a0580e29",
                    }
                }
            }
        },
        coverage={
            "api": ["PUT /lar/change"],
            "notes": "在已绑定 lar_change 记录上验证更新链路，确认远端标题、理由、金额与附件真正变化。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="lar_change",
        description="移民变更 domain 的闭环测试。基于主测试项目，依次覆盖 lar_change 的 create 与 update。",
        rounds=rounds,
    )
