"""ND0701: Workflow has no errorWorkflow setting configured."""

from pathlib import Path
from typing import Any

from valn8n_lib.rules.registry import SourceMap, register
from valn8n_lib.source_map import lookup_line
from valn8n_lib.types import Diagnostic, RuleCode, Severity, SuggestionLevel

_RULE_CODE = RuleCode("ND0701", Severity.WARNING)


@register(
    "ND0701",
    Severity.WARNING,
    description="No errorWorkflow configured",
    strict=False,
)
def check_missing_error_workflow(
    workflow: dict[str, Any],
    path: Path,
    source_map: SourceMap = None,
    suggestions: SuggestionLevel = SuggestionLevel.FULL,
) -> list[Diagnostic]:
    """Flag workflows that have no errorWorkflow setting."""
    settings = workflow.get("settings")
    if isinstance(settings, dict) and settings.get("errorWorkflow"):
        return []

    fname = str(path)
    pointer = "/settings"
    line = lookup_line(source_map, pointer)
    return [
        Diagnostic(
            file=fname,
            location=None,
            code=_RULE_CODE,
            message=(
                "Workflow has no errorWorkflow configured;"
                " failures will not trigger a fallback workflow"
            ),
            line=line,
        ),
    ]
