"""Layer 1 Validator.

Validates parsed CNL specifications against the physical invariants
defined in layer1_invariants.py.
"""

from layer1_invariants import ALL_INVARIANTS


def validate(_parsed_specs: list[dict], neuron_params: dict) -> dict:
    """Validate parsed CNL sentences against Layer 1 physical invariants.

    Parameters
    ----------
    _parsed_specs : list[dict]
        Reserved for future invariants that depend on parsed CNL sentences.
        Currently unused — invariants are purely parameter-based.
    neuron_params : dict
        Neuron parameters: tau, threshold, reset_potential, refractory_period,
        resting_potential.

    Returns
    -------
    dict
        Validation report with keys:
        - passed: list of invariant names that passed
        - failed: list of dicts with 'name' and 'reason' for each failure
        - overall: True if all passed, False if any failed
    """
    passed = []
    failed = []

    for name, invariant_fn in ALL_INVARIANTS.items():
        try:
            result = invariant_fn(neuron_params)
            if result:
                passed.append(name)
            else:
                failed.append({
                    "name": name,
                    "reason": f"Invariant '{name}' violated with params: {neuron_params}",
                })
        except KeyError as e:
            failed.append({
                "name": name,
                "reason": f"Missing parameter {e} required by invariant '{name}'",
            })

    return {
        "passed": passed,
        "failed": failed,
        "overall": len(failed) == 0,
    }
