from pathlib import Path

from brox.model import AIBom, Property, Service, create_aibom
from brox.scan.detectors.agent_frameworks import detect_agent_frameworks
from brox.scan.detectors.agent_tools import detect_agent_tools
from brox.scan.detectors.cloud_storage import detect_cloud_storage
from brox.scan.detectors.egress import detect_egress
from brox.scan.detectors.mcp import detect_mcp
from brox.scan.detectors.prompts import detect_prompts
from brox.scan.detectors.providers import detect_providers
from brox.scan.normalize import deduplicate_capabilities, normalize_scope


def scan_repository(repo_path: str) -> AIBom:
    repo_path_obj = Path(repo_path).resolve()

    print(f"Scanning repository: {repo_path_obj}")

    aibom = create_aibom(repo_root=str(repo_path_obj))

    all_components = []
    all_capabilities = []

    print("Detecting MCP servers...")
    mcp_components, mcp_capabilities = detect_mcp(repo_path_obj)
    all_components.extend(mcp_components)
    all_capabilities.extend(mcp_capabilities)
    print(f"  Found {len(mcp_components)} MCP components")

    print("Detecting prompts...")
    prompt_components, _ = detect_prompts(repo_path_obj)
    all_components.extend(prompt_components)
    print(f"  Found {len(prompt_components)} prompts")

    print("Detecting LLM providers...")
    provider_components, provider_capabilities = detect_providers(repo_path_obj)
    all_components.extend(provider_components)
    all_capabilities.extend(provider_capabilities)
    print(f"  Found {len(provider_components)} LLM providers")

    print("Detecting network egress...")
    egress_components, egress_capabilities = detect_egress(repo_path_obj)
    all_components.extend(egress_components)
    all_capabilities.extend(egress_capabilities)
    print(f"  Found {len(egress_components)} egress sinks")

    print("Detecting agent frameworks...")
    framework_components, _ = detect_agent_frameworks(repo_path_obj)
    all_components.extend(framework_components)
    print(f"  Found {len(framework_components)} agent frameworks")

    print("Detecting cloud storage...")
    storage_components, storage_capabilities = detect_cloud_storage(repo_path_obj)
    all_components.extend(storage_components)
    all_capabilities.extend(storage_capabilities)
    print(f"  Found {len(storage_components)} cloud storage integrations")

    print("Detecting agent tools...")
    tool_components, tool_capabilities = detect_agent_tools(repo_path_obj)
    all_components.extend(tool_components)
    all_capabilities.extend(tool_capabilities)
    print(f"  Found {len(tool_components)} registered agent tools")

    aibom.components.extend(all_components)

    print("Normalizing capabilities...")
    normalized_capabilities = []
    for cap in all_capabilities:
        cap.scope = normalize_scope(cap.scope)
        normalized_capabilities.append(cap)

    deduplicated_capabilities = deduplicate_capabilities(normalized_capabilities)
    print(f"  Total capabilities: {len(deduplicated_capabilities)}")

    if deduplicated_capabilities:
        capability_properties = []
        for cap in deduplicated_capabilities:
            capability_properties.append(
                Property(name="brox.capability.record", value=cap.to_record_string())
            )

        service = Service(name="brox.ai.capabilities", properties=capability_properties)
        aibom.services.append(service)

    return aibom
