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

    该类节点无法通过独立 create 接口创建，只能依赖项目恢复/创建时自动生成。
    因此这里验证：

    1. round_1_create
       - 通过恢复后的 project -> preparation 自动节点建立绑定；
    2. round_2_update
       - 修改 1 条未取得里程碑的 `is_exist`，验证 update 接口。
    """

    dataset_root = f"{config['project_root']}/tests/fixtures/auto_sync/datasource/preparation"
    filtered_root = f"{config['project_root']}/filtered_datasource/datasource/filtered"
    base_dataset_files = [
        "company_1.jsonl",
        "project_1.jsonl",
        "preparation_1.jsonl",
        "production_1.jsonl",
    ]
    remote_project_id = "f3e02e84-e81c-4aa6-88d4-f5aa108c8227"
    remote_preparation_id = "12e7e3e9-ca2d-4457-b89e-ee9e968ce50f"
    local_project_id = make_local_data_id("project", remote_project_id)
    local_preparation_id = make_local_data_id("preparation", remote_preparation_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="preparation",
        round_name="round_1_create",
        purpose="首轮通过项目恢复生成 preparation 节点并建立绑定",
        required_bindings={
            "project": [local_project_id],
            "preparation": [local_preparation_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
            "preparation": {local_preparation_id: remote_preparation_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "preparation": [local_preparation_id],
        },
        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="preparation",
        round_name="round_2_update",
        purpose="第二轮修改未取得里程碑的 is_exist 与 acquire_date，验证 preparation update",
        expected_changes={
            "preparation": {
                local_preparation_id: ["is_exist", "acquire_date"],
            }
        },
        required_bindings={
            "project": [local_project_id],
            "preparation": [local_preparation_id],
        },
        expected_binding_pairs={
            "project": {local_project_id: remote_project_id},
            "preparation": {local_preparation_id: remote_preparation_id},
        },
        require_distinct_binding_ids={
            "project": [local_project_id],
            "preparation": [local_preparation_id],
        },
        project_id_remap=project_id_remap,
        report_root=report_root,
    )
    rounds.append(round_2)

    return build_domain_report(
        domain="preparation",
        description="项目里程碑 domain 回归。通过数据库恢复后的项目自动节点建立绑定，再验证单条里程碑的有效更新。",
        rounds=rounds,
    )
