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"
REMOTE_LAR_ID = "718a9883-8479-4963-a58c-234d1984b117"
LOCAL_PROJECT_ID = make_local_data_id("project", REMOTE_PROJECT_ID)
LOCAL_LAR_ID = make_local_data_id("lar", REMOTE_LAR_ID)


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

    LAR 随项目自动生成，因此重点验证：

    1. round_1_create
       - 既有 LAR 节点可正常加载并建立绑定。
    2. round_2_update
       - 同时修改主信息、搬迁信息、投资信息三个分组字段，确认 3 条 update API 都能走通。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/lar"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
    ]
    local_lar_id = LOCAL_LAR_ID
    project_id_remap = {
        "remote_project_id": REMOTE_PROJECT_ID,
        "local_project_id": LOCAL_PROJECT_ID,
        "remap_node_types": {"project", "lar"},
    }

    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",
        round_name="round_1_create",
        purpose="首轮建立 LAR 基线绑定，确认项目级自动生成数据可被识别",
        required_bindings={
            "lar": [local_lar_id],
        },
        expected_bound_fields={
            "remote": {
                "lar": {
                    local_lar_id: {
                        "project_id": REMOTE_PROJECT_ID,
                        "completed_resettlement_pop": 83,
                        "completed_investment_amt": 24230.31,
                        "actual_rural_comp": 16644.28,
                    }
                }
            }
        },
        coverage={
            "notes": "确认 LAR 可从 project all_info.lar 中加载并建立绑定。",
        },
        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",
        round_name="round_2_update",
        purpose="第二轮同时修改主信息/搬迁/投资字段，验证 LAR 三条 update 子接口",
        expected_changes={
            "lar": {
                local_lar_id: [
                    "actual_rural_comp",
                    "completed_resettlement_pop",
                    "completed_investment_amt",
                ]
            }
        },
        required_bindings={
            "lar": [local_lar_id],
        },
        expected_bound_fields={
            "remote": {
                "lar": {
                    local_lar_id: {
                        "actual_rural_comp": 16645.28,
                        "completed_resettlement_pop": 84,
                        "completed_investment_amt": 24231.31,
                    }
                }
            }
        },
        coverage={
            "api": [
                "PUT /lar/main",
                "PUT /lar/resettlement",
                "PUT /lar/investment",
            ],
            "notes": "单轮同时命中 3 个 LAR 子 schema，确认 handler 分组更新逻辑与后端接口都可用。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="lar",
        description="移民征地 domain 的闭环测试。先绑定项目自动生成的 LAR 数据，再一次性验证主信息、搬迁、投资三条更新接口。",
        rounds=rounds,
    )
