"""
CLI output utilities - Rich-style terminal output.
"""
from typing import Dict


def format_size(size: int) -> str:
    """Format size in human-readable format."""
    for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
        if size < 1024:
            return f"{size:.1f} {unit}"
        size /= 1024
    return f"{size:.1f} PB"


def print_stats(stats: dict) -> None:
    """Print scanning statistics."""
    print("\n📊 Statistics")
    print("=" * 40)
    print(f"  Total Files:       {stats['total_files']:,}")
    print(f"  Total Size:        {format_size(stats['total_size'])}")
    print(f"  Duplicate Groups:  {stats['duplicate_groups']}")
    print(f"  Duplicate Files:   {stats['duplicate_files']}")
    print(f"  Wasted Space:      {format_size(stats['wasted_space'])}")


def print_table(duplicates: Dict) -> None:
    """Print duplicate groups in table format."""
    # Header
    print(f"{'Group':<8} {'Size':<10} {'Count':<6} {'Wasted':<12} {'Files'}")
    print("-" * 80)
    
    for i, (hash_val, group) in enumerate(duplicates.items(), 1):
        waste = format_size(group.total_waste())
        size = format_size(group.size)
        
        # First file
        first_path = group.files[0].path
        short_path = first_path[-50:] if len(first_path) > 50 else first_path
        
        print(f"{i:<8} {size:<10} {len(group.files):<6} {waste:<12} {short_path}")
        
        # Additional files (indented)
        for file_info in group.files[1:]:
            short_path = file_info.path[-50:] if len(file_info.path) > 50 else file_info.path
            print(f"{'':<8} {'':<10} {'':<6} {'':<12} └─ {short_path}")
        
        print()
