import click
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from .manager import ScoutManager

console = Console()

@click.group()
def main():
    """DataSovereign-Scout: Detect Ghost Data and Orphaned Datasets."""
    pass

@main.command()
@click.option('--manifest', '-m', required=True, type=click.Path(exists=True), help='Path to consent manifest (YAML)')
@click.option('--path', '-p', required=True, type=click.Path(exists=True), help='Directory to scan for data assets')
def scan(manifest, path):
    """Scan infrastructure and identify orphaned data."""
    console.print(f"[bold blue]Scouting for Ghost Data...[/bold blue]")
    console.print(f"Manifest: [cyan]{manifest}[/cyan]")
    console.print(f"Scan Path: [cyan]{path}[/cyan]\n")

    try:
        manager = ScoutManager(manifest, path)
        report = manager.run()
        
        # Show Results
        if report.orphans:
            console.print(Panel(f"[bold red]CRITICAL:[/bold red] Found {len(report.orphans)} orphaned datasets!", border_style="red"))
            
            table = Table(title="Orphaned Assets (Ghost Data)")
            table.add_column("Path", style="magenta")
            for o in report.orphans:
                table.add_row(o)
            console.print(table)
        else:
            console.print(Panel("[bold green]CLEAN:[/bold green] No orphaned datasets detected.", border_style="green"))

        if report.authorized_missing:
            console.print(f"\n[yellow]Note: {len(report.authorized_missing)} authorized assets were missing from the filesystem.[/yellow]")

        console.print(f"\n[bold]Total Scanned Files:[/bold] {report.total_scanned}")

    except Exception as e:
        console.print(f"[bold red]Error:[/bold red] {str(e)}")

if __name__ == "__main__":
    main()
