#  Copyright (C) 2026
#  ABM, Moscow
#
#  UNPUBLISHED PROPRIETARY MATERIAL.
#  ALL RIGHTS RESERVED.
#
#  Authors: Mike Orlov <m.orlov@abm-jsc.ru>
from typing import Mapping

from .._dataclass import FieldErrors
from .._exception import repr_exception


def repr_arg_name_to_exception(
        arg_name_to_exception: Mapping[str, Exception],
        arg_name_to_hint: Mapping[str, str] | None = None,
) -> list[str]:
    arg_name_to_hint = arg_name_to_hint or {}
    result = ['FATAL ARGUMENT ERRORS:']
    for i, (key, exception) in enumerate(arg_name_to_exception.items()):
        if isinstance(exception, FieldErrors):
            result.append(f'{i + 1}. {key}:')
            for j, field in enumerate(exception.field_to_error):
                sub_exception = exception.field_to_error[field]
                result.append(f'{j + 1}) {field} - {repr_exception(sub_exception)}')
        else:
            result.append(f'{i + 1}. {key} - {repr_exception(exception)}')
        if hint := arg_name_to_hint.get(key):
            result.append(f"@{key} hint:")
            result.append(hint)
            result.append(f"@{key} hint end")
    return result
