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]:
    """
    project domain 回归。

    使用 remap 后的本地 project ID，避免直接复用真实本地 ID，覆盖：

    1. 绑定项目基线
    2. `PUT /project` 与 `PUT /project/key_constraints`
    3. `PUT /project/complete_investment`
       `PUT /project/investment_decision`
       `PUT /project/dynamic_investment`
       `PUT /project/settlement_investment`
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/project"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    project_id_remap = {
        "remote_project_id": REMOTE_PROJECT_ID,
        "local_project_id": LOCAL_PROJECT_ID,
        "remap_node_types": {"project"},
        "project_update_direction": "push",
        "project_local_orphan_action": "none",
        "project_remote_orphan_action": "none",
    }

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

    round_1 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_1_bind",
        previous_dataset_dir=None,
        base_dataset_dir=filtered_root,
        base_dataset_files=["company_1.jsonl"],
        domain_name="project",
        round_name="round_1_bind",
        purpose="首轮建立 project 基线绑定，确认项目详情可正常加载",
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "project": [LOCAL_PROJECT_ID],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    LOCAL_PROJECT_ID: {
                        "name": "金沙江上游昌波水电站",
                        "manager_name": "杜光远",
                        "progress_type": "C",
                    }
                }
            }
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
        coverage={
            "api": ["GET /project", "GET /project/all_info"],
            "notes": "先验证 project 主记录与 all_info 可被正确加载并建立绑定。",
        },
    )
    rounds.append(round_1)

    round_2 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_2_base_info_update",
        previous_dataset_dir=f"{dataset_root}/round_1_bind",
        base_dataset_dir=filtered_root,
        base_dataset_files=["company_1.jsonl"],
        domain_name="project",
        round_name="round_2_base_info_update",
        purpose="第二轮修改项目基础信息与关键制约，验证 project 基础更新接口",
        expected_changes={
            "project": {
                LOCAL_PROJECT_ID: [
                    "short_name",
                    "manager_name",
                    "manager_phone",
                    "key_constraints",
                    "key_constraints_remark",
                ],
            }
        },
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "project": [LOCAL_PROJECT_ID],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    LOCAL_PROJECT_ID: {
                        "short_name": "昌波回归项目",
                        "manager_name": "自动化负责人A",
                        "manager_phone": "13900001234",
                        "key_constraints": "QT",
                        "key_constraints_remark": "自动化回归关键制约备注",
                    }
                }
            }
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
        coverage={
            "api": ["PUT /project", "PUT /project/key_constraints"],
            "notes": "同一轮同时命中项目基础信息与关键制约更新接口。",
        },
    )
    rounds.append(round_2)

    round_3 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_3_investment_update",
        previous_dataset_dir=f"{dataset_root}/round_2_base_info_update",
        base_dataset_dir=filtered_root,
        base_dataset_files=["company_1.jsonl"],
        domain_name="project",
        round_name="round_3_investment_update",
        purpose="第三轮修改投决/完成投资/动态投资/结算投资字段，验证剩余 4 个 project 更新接口",
        expected_changes={
            "project": {
                LOCAL_PROJECT_ID: [
                    "investment_decision_capacity",
                    "investment_decision_date",
                    "complete_investment",
                    "dynamic_investment",
                ],
            }
        },
        required_bindings={
            "project": [LOCAL_PROJECT_ID],
        },
        expected_binding_pairs={
            "project": {LOCAL_PROJECT_ID: REMOTE_PROJECT_ID},
        },
        require_distinct_binding_ids={
            "project": [LOCAL_PROJECT_ID],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    LOCAL_PROJECT_ID: {
                        "investment_decision_capacity": 83.1,
                        "investment_decision_date": "2021-08-15",
                        "complete_investment": 125000.0,
                        "dynamic_investment": 1413000.5,
                    }
                }
            }
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
        coverage={
            "api": [
                "PUT /project/complete_investment",
                "PUT /project/investment_decision",
                "PUT /project/dynamic_investment",
                "PUT /project/settlement_investment",
            ],
            "notes": "单轮覆盖 project 剩余 4 条投资类更新接口。当前回归仅校验后端可稳定读回的投资决策/完成投资/动态投资字段；结算投资接口仍保留覆盖，但扩展字段读回语义以后端修正为准。",
        },
    )
    rounds.append(round_3)

    return build_domain_report(
        domain="project",
        description="项目 domain 的闭环测试。通过 remap 后的本地 project，覆盖绑定与 6 条 project 更新接口，并使用当前测试账号可访问的项目基线。",
        rounds=rounds,
    )