from rich import print
from pathlib import Path
import os
import shutil


def main(debug=False):
    source = Path(os.getcwd())

    print(f"[bold blue]DIRECTORY SCAN: [/bold blue][bold yellow]{source}[/bold yellow]")
    print(f"[bold blue]Found: [/bold blue][bold green]{len(source.iterdir())}[/bold green] [bold yellow]items[/bold yellow]")
    
    if not source.exists():
        print("[bold red]Directory not found![/bold red]")
        return

    if debug:
        print(f"[bold yellow][DEBUG][/bold yellow] Source directory: {source}")

    files = {}

    # Scan directory
    for item in source.iterdir():

        if item.is_file():
            ext = item.suffix.lstrip(".").lower() or "No Extension"
            files.setdefault(ext, []).append(item)

            if debug:
                print(f"[bold yellow][DEBUG][/bold yellow] Found file: {item.name} (.{ext})")

        elif item.is_dir():
            files.setdefault("DIRECTORY", []).append(item)

            if debug:
                count = len(list(item.iterdir()))
                print(f"[bold yellow][DEBUG][/bold yellow] Found directory: {item.name} ({count} items)")

    # Display
    for extension in sorted(files):
        print(f"[bold red].{extension}[/bold red]")
        print("-" * 40)

        for i, item in enumerate(sorted(files[extension]), start=1):
            if extension == "DIRECTORY":
                count = len(list(item.iterdir()))
                print(f"[bold cyan]{i}.[/bold cyan] [bold green]{item.name} ({item}) ({count} items)[/bold green]")
            else:
                size = item.stat().st_size
                print(f"[bold cyan]{i}.[/bold cyan] [bold green]{item.name} ({item}) ({size} bytes)[/bold green]")

        print()

    if debug:
        print(f"[bold yellow][DEBUG][/bold yellow] Display complete.")

    