"""
Project: Deleting Unneeded Files
Page 247
"""

import sys
from pathlib import Path


def get_path():
    if len(sys.argv) != 2:
        print("ERROR: solo un argumento es necesario")
        sys.exit(1)
    path = Path(sys.argv[1])
    if not path.is_dir():
        print(f"ERROR: '{path}' no es una ruta a un directorio")
        sys.exit(1)
    return path.resolve()


root = get_path()
size_max = 100
big_files = []

for path in list(root.glob("**/*")):
    size_mb = round(path.lstat().st_size / (1024*1024), 2)
    if size_mb >= size_max:
        big_files.append({"size": size_mb, "path": path})

print(f"Mostrando archivos >= a {size_max}MB:")
# print(f"Eliminando archivos >= a {size_max}MB:")
big_files = sorted(big_files, reverse=True, key=lambda f: f["size"])
biggest = str(format(big_files[0]["size"], ","))
for file in big_files:
    size = str(format(file["size"], ","))
    space = " " * (len(biggest) - len(size))
    print(f"[{space}{size}] {file['path']}")
    # file["path"].unlink()  # PELIGRO: eliminará el archivo
