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

    这个 domain 重点不是数量覆盖，而是把两个 update 接口都至少打通一次：

    1. ConstructionUpdate
       - 用 `is_crucial` 变化验证基础信息更新。

    2. ConstructionPlanUpdate
       - 用 `plan_start_date`
       - `plan_complete_date`
       - `contract_quantity`
       - `plan_node`
       验证计划信息更新。

    因此这里只保留两轮：

    - round_1_create: 创建 1 条施工任务。
    - round_2_update: 同一条任务同时修改基础信息和计划信息。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/construction_task"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    local_task_id = "0200848d-d9e7-4372-a6f6-f768bac13955"
    project_id_remap = {
        "remote_project_id": REMOTE_PROJECT_ID,
        "local_project_id": LOCAL_PROJECT_ID,
        "remap_node_types": {"project"},
    }
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
        "contract_1.jsonl",
        "construction_task_1.jsonl",
    ]

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

    # 用例 1：先把施工任务创建到远端，并确认绑定建立成功。
    round_1 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_1_create",
        previous_dataset_dir=None,
        domain_name="construction_task",
        round_name="round_1_create",
        purpose="首轮创建施工任务",
        required_bindings={"construction_task": [local_task_id]},
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_1)

    # 用例 2：同一条任务同时修改基础字段和计划字段，覆盖两个 update 接口。
    round_2 = run_pipeline_round(
        config=config,
        dataset_dir=f"{dataset_root}/round_2_update",
        previous_dataset_dir=f"{dataset_root}/round_1_create",
        domain_name="construction_task",
        round_name="round_2_update",
        purpose="第二轮修改关键任务标记和计划节点，验证 base update + plan update",
        expected_changes={
            "construction_task": {
                local_task_id: [
                    "is_crucial",
                    "plan_start_date",
                    "plan_complete_date",
                    "contract_quantity",
                    "plan_node",
                ]
            }
        },
        required_bindings={"construction_task": [local_task_id]},
        base_dataset_dir=filtered_root,
        base_dataset_files=base_dataset_files,
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="construction_task",
        description="施工任务 domain 的最小闭环测试。先创建 1 条任务，再在第二轮同时覆盖基础信息更新和计划信息更新。",
        rounds=rounds,
    )
