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]:
    """
    personnel domain 的闭环回归。

    选 1 条真实存在的 personnel 基线，重点覆盖：

    1. round_1_create
       - 建立 project / contract / personnel 绑定；
       - 确认 personnel 基线可被正常识别。
    2. round_2_update
       - 同时修改基础字段 `contract_id` 与计划字段；
       - 显式验证当前 handler 的 personnel update 路径能把计划字段推到远端。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/personnel"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
        "contract_1.jsonl",
    ]
    local_personnel_id = "2006708a-dae4-4874-9207-22889cf2c886"
    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="personnel",
        round_name="round_1_create",
        purpose="首轮建立 personnel 基线绑定，确认 project/contract 依赖链可用",
        required_bindings={
            "personnel": [local_personnel_id],
        },
        expected_bound_fields={
            "remote": {
                "personnel": {
                    local_personnel_id: {
                        "code": "personnel",
                        "project_id": REMOTE_PROJECT_ID,
                    }
                }
            }
        },
        coverage={
            "api": ["GET /personnel/list"],
            "notes": "确认 personnel 基线能被远端正确加载并建立绑定。",
        },
        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="personnel",
        round_name="round_2_update",
        purpose="第二轮同时修改 personnel 的基础字段与计划字段，验证 update 路径",
        expected_changes={
            "personnel": {
                local_personnel_id: [
                    "contract_id",
                    "plan_start_date",
                    "plan_start_quantity",
                    "plan_exit_date",
                    "plan_node",
                ]
            }
        },
        required_bindings={
            "personnel": [local_personnel_id],
        },
        expected_bound_fields={
            "remote": {
                "personnel": {
                    local_personnel_id: {
                        "plan_start_date": "2024-05-01",
                        "plan_start_quantity": 12,
                        "plan_exit_date": "2026-12-31",
                        "plan_node.0.date": "2024-06-01",
                        "plan_node.1.quantity": 20.0,
                        "plan_node.2.date": "2026-06-30",
                    }
                }
            }
        },
        coverage={
            "api": ["PUT /personnel/update"],
            "notes": "当前 handler 将基础更新与计划更新都走 personnel update 路径；本轮显式校验远端计划字段是否真正变化。",
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="personnel",
        description="人员 domain 的闭环测试。以 1 条真实 personnel 基线为核心，先建立绑定，再在第二轮同时验证基础字段更新与计划字段更新。",
        rounds=rounds,
    )
