"""
Engine check registry — discovers and runs all check modules.

Each check module registers its checks via register_checks().
The CLI (engine_check.py) imports ALL_CHECKS from here.
"""

# Global registry: list of (key, name, function, category)
CHECK_REGISTRY = []

# Section aliases mapping section names to lists of check keys
SECTION_MAP = {}

# Quick-mode check keys
QUICK_KEYS = set()

# Fix registry: dict of check_key -> fix_function
FIX_REGISTRY = {}


def register_check(key, name, fn, category=None, quick=False):
    """Register a single check function.

    Args:
        key: Short identifier (e.g., "chain", "route_coverage")
        name: Human-readable name (e.g., "Skill -> Agent -> Validator Chain")
        fn: Check function with signature fn(base, discovered) -> {"pass": [], "fail": [], "warn": []}
        category: Category for --section grouping (e.g., "structural", "gui", "security")
        quick: If True, include in --quick mode
    """
    CHECK_REGISTRY.append((key, name, fn, category))
    if quick:
        QUICK_KEYS.add(key)


def register_section(name, keys):
    """Register a section alias that maps to multiple check keys.

    Args:
        name: Section name for --section flag (e.g., "syntax", "logic")
        keys: List of check keys this section includes
    """
    SECTION_MAP[name] = keys


def get_all_checks():
    """Return list of (key, name, fn) tuples — compatible with old ALL_CHECKS format."""
    return [(key, name, fn) for key, name, fn, _cat in CHECK_REGISTRY]


def get_section_map():
    """Return the full section map including per-check and alias sections."""
    result = {key: [key] for key, _, _, _ in CHECK_REGISTRY}
    result.update(SECTION_MAP)
    return result


def get_quick_keys():
    """Return the set of check keys to run in --quick mode."""
    return QUICK_KEYS


def get_category_keys(category):
    """Return all check keys belonging to a given category."""
    return [key for key, _, _, cat in CHECK_REGISTRY if cat == category]


def register_fix(key, fn):
    """Register a fix function for a check key.

    Args:
        key: Check key this fix addresses
        fn: Fix function with signature fn(base, discovered) -> {"fixed": [], "skipped": []}
    """
    FIX_REGISTRY[key] = fn


def get_fixes_for_keys(keys):
    """Return {key: fix_fn} for all keys that have registered fixes."""
    return {k: FIX_REGISTRY[k] for k in keys if k in FIX_REGISTRY}


# ── Import all check modules to trigger registration ──
# Order matters: structural first, then semantic, then deep_logic,
# then new categories. This preserves check numbering.
from . import structural    # checks 1-6
from . import semantic      # checks 7-18
from . import deep_logic    # checks 19-23

# New check categories
from . import gui_integrity    # checks 24-30
from . import visual_pipeline  # checks 31-38
from . import dead_code        # checks 39-42
from . import documentation    # checks 43-49
from . import security         # checks 50-53
