from __future__ import annotations

from typing import Any

from . import project_model


WorkflowStep = dict[str, Any]


# @hook B7 20260804
# Provides project-type workflow guides and infers completion from shared project results.
WORKFLOWS: dict[str, list[WorkflowStep]] = {
    "bridge": [
        {"stepID": "BRG-01", "name": "流域参数", "programID": "1-01"},
        {"stepID": "BRG-02", "name": "设计暴雨", "programID": "1-01"},
        {"stepID": "BRG-03", "name": "产汇流", "programID": "1-04"},
        {"stepID": "BRG-04", "name": "设计洪水", "programID": "1-10"},
        {"stepID": "BRG-05", "name": "水面线", "programID": "4-01"},
        {"stepID": "BRG-06", "name": "阻水比", "programID": "3-15"},
        {"stepID": "BRG-07", "name": "壅水", "programID": "3-16"},
        {"stepID": "BRG-08", "name": "冲刷", "programID": "3-13"},
        {"stepID": "BRG-09", "name": "净空", "programID": "3-17"},
        {"stepID": "BRG-10", "name": "影响评价", "programID": "3-14"},
        {"stepID": "BRG-11", "name": "报告", "programID": "RPT-01"},
    ],
    "tunnel": [
        {"stepID": "TUN-01", "name": "流域参数", "programID": "1-01"},
        {"stepID": "TUN-02", "name": "设计洪水", "programID": "1-10"},
        {"stepID": "TUN-03", "name": "水面线", "programID": "4-01"},
        {"stepID": "TUN-04", "name": "明洞壅水影响", "programID": "3-14"},
        {"stepID": "TUN-05", "name": "冲刷复核", "programID": "3-13"},
        {"stepID": "TUN-06", "name": "基础埋深安全", "programID": "3-12"},
        {"stepID": "TUN-07", "name": "净空校核", "programID": "3-17"},
        {"stepID": "TUN-08", "name": "报告", "programID": "RPT-01"},
    ],
    "levee": [
        {"stepID": "LEV-01", "name": "流域参数", "programID": "1-01"},
        {"stepID": "LEV-02", "name": "设计洪水", "programID": "1-10"},
        {"stepID": "LEV-03", "name": "水面线", "programID": "4-01"},
        {"stepID": "LEV-04", "name": "堤顶超高", "programID": "3-11"},
        {"stepID": "LEV-05", "name": "回水影响", "programID": "3-14"},
        {"stepID": "LEV-06", "name": "影响评价", "programID": "3-14"},
        {"stepID": "LEV-07", "name": "报告", "programID": "RPT-01"},
    ],
    "reservoir": [
        {"stepID": "RSV-01", "name": "流域参数", "programID": "1-01"},
        {"stepID": "RSV-02", "name": "水库洪水总量", "programID": "1-08"},
        {"stepID": "RSV-03", "name": "设计洪水成果", "programID": "1-10"},
        {"stepID": "RSV-04", "name": "调洪演算", "programID": "5-01"},
        {"stepID": "RSV-05", "name": "坝顶超高", "programID": "5-04"},
        {"stepID": "RSV-06", "name": "库容复核", "programID": "5-08"},
        {"stepID": "RSV-07", "name": "报告", "programID": "RPT-01"},
    ],
}

PROJECT_TYPE_ALIASES = {
    "桥梁": "bridge",
    "桥梁洪评": "bridge",
    "明洞": "tunnel",
    "隧洞": "tunnel",
    "堤防": "levee",
    "河道堤防": "levee",
    "水库": "reservoir",
    "调洪": "reservoir",
}


def get_workflow(
    project_type: str,
    project: project_model.ProjectData | None = None,
    *,
    case_id: str = "proposed",
    frequency: float | str = 1.0,
) -> dict[str, Any]:
    key = _normalize_project_type(project_type)
    completed = (
        project_model.get_completed_programs(project, case_id, frequency)
        if project is not None
        else set()
    )
    steps: list[WorkflowStep] = []
    for raw in WORKFLOWS[key]:
        step = dict(raw)
        step["isComplete"] = bool(step["programID"] in completed)
        steps.append(step)
    return {
        "projectType": key,
        "steps": steps,
        "completed": sum(1 for step in steps if step["isComplete"]),
        "total": len(steps),
    }


def list_workflow_types() -> list[str]:
    return list(WORKFLOWS)


def _normalize_project_type(project_type: str) -> str:
    key = str(project_type or "").strip()
    key = PROJECT_TYPE_ALIASES.get(key, key.lower())
    if key not in WORKFLOWS:
        key = "bridge"
    return key
