"""asa lint — Boundary, contract, structure, security enforcement."""

import typer

from ..core import logging as log
from ..core.config import load_config
from ..core.errors import AsaError
from ..core.paths import find_project_root, parse_domain_slice


def run_lint(target: str | None, *, strict: bool = False) -> None:
    """Lint project or single slice for violations."""
    from ..engine_loader import require_engine
    require_engine()
    from ..engine.linter.linter import lint_project, lint_slice
    try:
        project_root = find_project_root()
        config = load_config(project_root)

        if target:
            domain, slice_name = parse_domain_slice(target)
            exit_code = lint_slice(domain, slice_name, project_root, config, strict=strict)
        else:
            exit_code = lint_project(project_root, config, strict=strict)

        raise typer.Exit(exit_code)

    except AsaError as e:
        log.error(str(e))
        raise typer.Exit(1)
    except typer.Exit:
        raise
    except Exception as e:
        log.error(f"[INTERNAL ERROR] {e}")
        raise typer.Exit(2)
