"""Dental compliance frameworks with regulation-mapped checks."""

from __future__ import annotations

from typing import Any

from .osha_dental import OSHA_DENTAL_CONTROLS
from .cdc_infection import CDC_INFECTION_CONTROLS
from .hipaa_dental import HIPAA_DENTAL_CONTROLS
from .radiation_safety import RADIATION_SAFETY_CONTROLS
from .state_boards import STATE_BOARD_CONTROLS
from .sterilization import STERILIZATION_CONTROLS

FRAMEWORKS: dict[str, dict[str, Any]] = {
    "osha_dental": OSHA_DENTAL_CONTROLS,
    "cdc_infection": CDC_INFECTION_CONTROLS,
    "hipaa_dental": HIPAA_DENTAL_CONTROLS,
    "radiation_safety": RADIATION_SAFETY_CONTROLS,
    "state_boards": STATE_BOARD_CONTROLS,
    "sterilization": STERILIZATION_CONTROLS,
}

FRAMEWORK_NAMES: dict[str, str] = {
    "osha_dental": "OSHA Dental Standards",
    "cdc_infection": "CDC Infection Control for Dental Settings",
    "hipaa_dental": "HIPAA for Dental Practices",
    "radiation_safety": "Dental Radiation Safety",
    "state_boards": "State Dental Board Regulations",
    "sterilization": "Sterilization Protocols",
}


def get_all_checks(framework: str) -> list[dict[str, Any]]:
    """Flatten a framework's controls into a list of individual checks."""
    controls = FRAMEWORKS.get(framework, {})
    checks: list[dict[str, Any]] = []
    for control_id, control in controls.items():
        for check in control.get("checks", []):
            checks.append({
                "control_id": control_id,
                "control_name": control["name"],
                "framework": framework,
                "regulation_reference": control.get("regulation_reference", ""),
                **check,
            })
    return checks


def get_checks_for_practice_type(
    framework: str, practice_type: str
) -> list[dict[str, Any]]:
    """Get checks filtered by practice type."""
    all_checks = get_all_checks(framework)
    return [
        c for c in all_checks
        if "all" in c.get("practice_types", ["all"])
        or practice_type in c.get("practice_types", ["all"])
    ]


def get_checks_for_state(
    framework: str, state: str
) -> list[dict[str, Any]]:
    """Get checks filtered by state (for state_boards framework)."""
    all_checks = get_all_checks(framework)
    return [
        c for c in all_checks
        if not c.get("states") or state.upper() in c.get("states", [])
    ]
