from __future__ import annotations


if __package__ in {None, ""}:  # pragma: no cover - permite ejecución como script
    import sys
    from pathlib import Path

    _current_file = Path(__file__).resolve()
    for _parent in (_current_file.parent, *_current_file.parents):
        if (_parent / "pyproject.toml").exists() or (_parent / ".git").exists():
            _project_root = _parent
            break
    else:
        _project_root = _current_file.parents[2]
    _src_root = _project_root / "src"
    for _path in (_project_root, _src_root):
        _path_str = str(_path)
        if _path_str not in sys.path:
            sys.path.insert(0, _path_str)
    from agix._script_utils import bootstrap_script_environment

    bootstrap_script_environment("__main__", str(_current_file))

"""Interface simple para evaluar alineamiento ético."""

from typing import Dict, Iterable, Tuple

from agix.qualia.ecoethics import EcoEthics
from .frameworks import (
    DeontologicalEthics,
    UtilitarianEthics,
    VirtueEthics,
)


class AlignmentInterface:
    """Evalúa acciones usando uno o varios marcos éticos."""

    def __init__(self, frameworks: Iterable | None = None):
        if frameworks is None:
            frameworks = [EcoEthics()]
        elif not isinstance(frameworks, (list, tuple)):
            frameworks = [frameworks]
        self.frameworks = list(frameworks)

    def judge(self, action: Dict[str, float]):
        """Devuelve los resultados de evaluación.

        Si sólo hay un marco configurado, retorna ``(score, clasificacion)``.
        Con múltiples marcos se devuelve un diccionario ``{nombre: (score, label)}``.
        """

        results = {}
        for marco in self.frameworks:
            score = marco.evaluar(action)
            label = marco.clasificar(score)
            results[marco.__class__.__name__] = (score, label)

        return next(iter(results.values())) if len(results) == 1 else results
