"""Table rendering helpers for the CLI."""

from __future__ import annotations

from rich.table import Table

from revo_module_installer.config.manifest import ManifestEntry
from revo_module_installer.config.registry import RegistryEntry


def build_registry_table(
    registry_entries: list[RegistryEntry],
    *,
    show_description: bool,
) -> Table:
    """Build a rich table for registry entries.

    Args:
        registry_entries: Entries from the registry index.
        show_description: Whether to show descriptions.

    Returns:
        Rich table representation of entries.
    """
    table: Table = Table(title="Registry entries")
    table.add_column("ID", style="cyan", no_wrap=True)
    table.add_column("Name", style="bold")
    if show_description:
        table.add_column("Description")
    table.add_column("Manifest URL", overflow="fold")

    for registry_entry in registry_entries:
        row: list[str] = [registry_entry.id, registry_entry.name]
        if show_description:
            row.append(registry_entry.description)
        row.append(registry_entry.manifest_url)
        table.add_row(*row)
    return table


def build_search_table(registry_entries: list[RegistryEntry]) -> Table:
    """Build a search-focused table for registry entries.

    Args:
        registry_entries: Entries from the registry index.

    Returns:
        Rich table representation for searching entries.
    """
    table: Table = Table(title="Available modules")
    table.add_column("Name", style="bold")
    table.add_column("Description")
    for registry_entry in registry_entries:
        table.add_row(registry_entry.name, registry_entry.description)
    return table


def render_manifest_table(manifest_entries: list[ManifestEntry]) -> Table:
    """Build a rich table for manifest entries.

    Args:
        manifest_entries: Entries from the manifest index.

    Returns:
        Rich table representation of manifest entries.
    """
    table: Table = Table(title="Manifest actions")
    table.add_column("ID", style="cyan", no_wrap=True)
    table.add_column("Action", style="magenta")
    table.add_column("Artifact", overflow="fold")
    table.add_column("Target", overflow="fold")
    table.add_column("Ref", overflow="fold")
    table.add_column("Match Mode", overflow="fold")

    for manifest_entry in manifest_entries:
        ref_summary_parts: list[str] = []
        if manifest_entry.ref_type:
            ref_summary_parts.append(manifest_entry.ref_type)
        if manifest_entry.ref_value:
            ref_summary_parts.append(manifest_entry.ref_value)
        ref_summary: str = ":".join(ref_summary_parts) if ref_summary_parts else "-"
        target_path: str = manifest_entry.target_path or manifest_entry.artifact_path
        match_mode_type: str = "-"
        if manifest_entry.match_mode is not None:
            match_mode_type = manifest_entry.match_mode.type
        table.add_row(
            manifest_entry.id,
            manifest_entry.action,
            manifest_entry.artifact_path,
            target_path,
            ref_summary,
            match_mode_type,
        )
    return table
