"""asa slice analyze — Detailed slice analysis."""

import typer

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


def run_slice_analyze(domain_slice: str) -> None:
    """Analyze a single slice: LOC, imports, coupling, complexity."""
    from ...engine_loader import require_engine
    require_engine()
    from ...engine.splitter.splitter import analyze_slice
    try:
        domain, slice_name = parse_domain_slice(domain_slice)
        project_root = find_project_root()
        slice_path = resolve_slice_path(domain, slice_name, project_root)

        if not slice_path.exists():
            log.error(f"Slice not found: {domain}/{slice_name}")
            raise typer.Exit(1)

        analysis = analyze_slice(slice_path, domain, slice_name)

        log.header(f"Slice Analysis: {domain}/{slice_name}")
        log.info(f"  Total LOC: {analysis['total_loc']}")
        log.info(f"  Public functions: {analysis['public_functions']}")
        if analysis["function_names"]:
            for fn in analysis["function_names"]:
                log.detail(f"    {fn}")
        log.info(f"  Responsibilities detected: {analysis['responsibilities_detected']}")
        if analysis["responsibility_prefixes"]:
            log.detail(f"    Prefixes: {', '.join(analysis['responsibility_prefixes'])}")
        log.info(f"  Cross-domain imports: {analysis['cross_domain_imports']}")
        if analysis["cross_domain_details"]:
            for detail in analysis["cross_domain_details"]:
                log.detail(f"    {detail}")
        log.info(f"  Oversized: {'⚠️ YES' if analysis['is_oversized'] else '✅ No'}")

    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)
