from __future__ import annotations

from typing import Any

from auto_test_domains.base import build_domain_report, make_local_data_id, run_pipeline_round


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

     目标：覆盖 `project_detail_data` 当前支持的全部 6 个 key 及其对应更新接口：

     - `plan_construction` → `PUT /project/plan_construction`
     - `actual_construction` → `PUT /project/actual_construction`
     - `ensure_production` → `PUT /project/ensure_capacity`
     - `climb_production` → `PUT /project/climb_capacity`
    - `challenge_production` → `PUT /project/challenge_capacity`

    `ensure/climb/challenge` 默认策略是 pull，本用例会在临时 profile 中仅对当前 key 切到 push，
     以确认对应后端更新接口真实可用，并在报告中展示字段校验结果。

    `production` 当前按文档约束不混入这组统一回归：
    后端确认该接口会直接更新 `project` 字段，但不保证创建/更新可轮询的
    `project_detail_data.production` 记录，需单列方案再测。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/project_detail_data"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
    ]
    remote_project_id = "f3e02e84-e81c-4aa6-88d4-f5aa108c8227"
    remote_actual_construction_id = "c99ae2e4-809e-47dd-8b86-61674b6a5bea"
    seed_ensure_production_id = "11111111-1111-4111-8111-111111111111"
    seed_climb_production_id = "22222222-2222-4222-8222-222222222222"
    seed_challenge_production_id = "33333333-3333-4333-8333-333333333333"
    seed_plan_construction_id = "4bc5e9db-c9dc-41f1-a77d-574b4fe37b01"
    local_project_id = make_local_data_id("project", remote_project_id)
    local_actual_construction_id = make_local_data_id("project_detail_data", remote_actual_construction_id)
    local_plan_construction_id = make_local_data_id("project_detail_data", seed_plan_construction_id)
    local_ensure_production_id = make_local_data_id("project_detail_data", seed_ensure_production_id)
    local_climb_production_id = make_local_data_id("project_detail_data", seed_climb_production_id)
    local_challenge_production_id = make_local_data_id("project_detail_data", seed_challenge_production_id)
    project_id_remap = {
        "remote_project_id": remote_project_id,
        "local_project_id": local_project_id,
        "remap_node_types": {"project", "preparation", "production", "project_detail_data"},
    }
    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="project_detail_data",
        round_name="round_1_create",
        purpose="首轮通过项目恢复生成 project_detail_data 节点并建立绑定",
        required_bindings={
            "project": [local_project_id],
            "project_detail_data": [local_actual_construction_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "project_detail_data": [local_actual_construction_id],
        },
        expected_bound_fields={
            "remote": {
                "project_detail_data": {
                    local_actual_construction_id: {
                        "key": "actual_construction",
                        "value.0.capacity": 82.6,
                        "value.0.date": "2023-09-25",
                    }
                }
            }
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
        coverage={
            "keys": ["actual_construction"],
            "api": ["PUT /project/actual_construction"],
            "notes": "建立 project / actual_construction 绑定并确认基线明细加载正确。",
        },
    )
    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="project_detail_data",
        round_name="round_2_update",
        purpose="第二轮修改 actual_construction 明细容量，验证 project_detail update",
        expected_changes={
            "project_detail_data": {
                local_actual_construction_id: ["value"],
            },
            "project": {
                local_project_id: [
                    "actual_construction_capacity",
                    "actual_construction_date",
                    "accumulated_construction_capacity",
                ],
            }
        },
        required_bindings={
            "project": [local_project_id],
            "project_detail_data": [local_actual_construction_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "project_detail_data": [local_actual_construction_id],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    local_project_id: {
                        "actual_construction_capacity": 5.5,
                        "actual_construction_date": "2026-02-18",
                        "accumulated_construction_capacity": 5.5,
                    }
                },
                "project_detail_data": {
                    local_actual_construction_id: {
                        "key": "actual_construction",
                        "value.0.capacity": 5.5,
                        "value.0.date": "2026-02-18",
                    }
                },
            }
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
        coverage={
            "keys": ["actual_construction"],
            "api": ["PUT /project/actual_construction"],
            "notes": "确认实际开工接口会同步更新 project 实际开工日期/容量与累计开工容量。",
        },
    )
    rounds.append(round_2)

    round_3 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_3_plan_construction_create",
        previous_dataset_dir=f"{dataset_root}/round_2_update",
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        domain_name="project_detail_data",
        round_name="round_3_plan_construction_create",
        purpose="创建并推送 plan_construction，验证 /project/plan_construction 及 project 字段联动",
        expected_changes={
            "project": {
                local_project_id: ["plan_construction_capacity", "plan_construction_date"],
            },
        },
        required_bindings={
            "project": [local_project_id],
            "project_detail_data": [local_plan_construction_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "project_detail_data": [local_plan_construction_id],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    local_project_id: {
                        "plan_construction_capacity": 6.6,
                        "plan_construction_date": "2026-02-20",
                    }
                },
                "project_detail_data": {
                    local_plan_construction_id: {
                        "key": "plan_construction",
                        "value.0.capacity": 6.6,
                        "value.0.date": "2026-02-20",
                    }
                },
            }
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
        coverage={
            "keys": ["plan_construction"],
            "api": ["PUT /project/plan_construction"],
            "notes": "确认计划开工接口可以创建 detail，并同步 project 计划开工字段。",
        },
    )
    rounds.append(round_3)

    round_5 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_5_ensure_production_push",
        previous_dataset_dir=f"{dataset_root}/round_3_plan_construction_create",
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        domain_name="project_detail_data",
        round_name="round_5_ensure_production_push",
        purpose="推送 ensure_production，验证 /project/ensure_capacity 接口与 project.ensure_production_capacity 联动",
        expected_changes={
            "project": {
                local_project_id: ["ensure_production_capacity"],
            },
        },
        required_bindings={
            "project": [local_project_id],
            "project_detail_data": [local_ensure_production_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "project_detail_data": [local_ensure_production_id],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    local_project_id: {
                        "ensure_production_capacity": 21.0,
                    }
                },
                "project_detail_data": {
                    local_ensure_production_id: {
                        "key": "ensure_production",
                        "value.0.year": 2025,
                        "value.0.capacity": 51.0,
                        "value.1.year": 2026,
                        "value.1.capacity": 21.0,
                    }
                },
            }
        },
        project_id_remap=project_id_remap,
        project_detail_push_keys={"ensure_production"},
        report_root=report_root,
        coverage={
            "keys": ["ensure_production"],
            "api": ["PUT /project/ensure_capacity"],
            "notes": "临时将 ensure_production 从 pull 切为 push，仅用于确认后端更新接口可测通。",
        },
    )
    rounds.append(round_5)

    round_6 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_6_climb_production_push",
        previous_dataset_dir=f"{dataset_root}/round_5_ensure_production_push",
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        domain_name="project_detail_data",
        round_name="round_6_climb_production_push",
        purpose="推送 climb_production，验证 /project/climb_capacity 接口与 project.climb_production_capacity 联动",
        expected_changes={
            "project": {
                local_project_id: ["climb_production_capacity"],
            },
        },
        required_bindings={
            "project": [local_project_id],
            "project_detail_data": [local_climb_production_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "project_detail_data": [local_climb_production_id],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    local_project_id: {
                        "climb_production_capacity": 22.0,
                    }
                },
                "project_detail_data": {
                    local_climb_production_id: {
                        "key": "climb_production",
                        "value.0.capacity": 102.0,
                        "value.1.capacity": 22.0,
                        "value.0.year": 2025,
                        "value.1.year": 2026,
                    }
                },
            }
        },
        project_id_remap=project_id_remap,
        project_detail_push_keys={"climb_production"},
        report_root=report_root,
        coverage={
            "keys": ["climb_production"],
            "api": ["PUT /project/climb_capacity"],
            "notes": "验证登高投产接口，同时覆盖后端对 climb >= ensure 的约束路径。",
        },
    )
    rounds.append(round_6)

    round_7 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_7_challenge_production_push",
        previous_dataset_dir=f"{dataset_root}/round_6_climb_production_push",
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        domain_name="project_detail_data",
        round_name="round_7_challenge_production_push",
        purpose="推送 challenge_production，验证 /project/challenge_capacity 接口与 project.challenge_production_capacity 联动",
        expected_changes={
            "project": {
                local_project_id: ["challenge_production_capacity"],
            },
        },
        required_bindings={
            "project": [local_project_id],
            "project_detail_data": [local_challenge_production_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "project_detail_data": [local_challenge_production_id],
        },
        expected_bound_fields={
            "remote": {
                "project": {
                    local_project_id: {
                        "challenge_production_capacity": 23.0,
                    }
                },
                "project_detail_data": {
                    local_challenge_production_id: {
                        "key": "challenge_production",
                        "value.0.capacity": 102.0,
                        "value.1.capacity": 23.0,
                        "value.0.year": 2025,
                        "value.1.year": 2026,
                    }
                },
            }
        },
        project_id_remap=project_id_remap,
        project_detail_push_keys={"challenge_production"},
        report_root=report_root,
        coverage={
            "keys": ["challenge_production"],
            "api": ["PUT /project/challenge_capacity"],
            "notes": "验证揭榜投产接口，同时覆盖后端对 challenge >= climb 的约束路径。",
        },
    )
    rounds.append(round_7)

    return build_domain_report(
        domain="project_detail_data",
        description="项目详情 domain 回归。覆盖 plan_construction / actual_construction / ensure_production / climb_production / challenge_production 五类 key，并在报告中展示对应接口与 project 字段联动校验。production 按文档约束单列，不混入本组统一回归。",
        rounds=rounds,
    )
