import os
import shutil

# ---------------------
# CONFIGURATION
# ---------------------
dossier_folder = "C:\\Users\\Antoine\\PycharmProjects\\PHOTO\\dossier"

# ---------------------
# INPUT
# ---------------------
user_input = input("Entrez le(s) numéro(s) de dossier à supprimer (ex: 2 ou 2,4,5) : ")
to_delete = set(int(x.strip()) for x in user_input.split(","))

# ---------------------
# LISTER LES DOSSIERS EXISTANTS TRIÉS
# ---------------------
existing = sorted(
    int(f) for f in os.listdir(dossier_folder)
    if os.path.isdir(os.path.join(dossier_folder, f)) and f.isdigit()
)

# ---------------------
# SUPPRIMER LES DOSSIERS DEMANDÉS
# ---------------------
for num in to_delete:
    folder_path = os.path.join(dossier_folder, str(num))
    if os.path.exists(folder_path):
        shutil.rmtree(folder_path)
        print(f"Dossier {num} supprimé.")
    else:
        print(f"Dossier {num} introuvable, ignoré.")

# ---------------------
# RENOMMER LES DOSSIERS RESTANTS EN ORDRE
# ---------------------
remaining = sorted(
    int(f) for f in os.listdir(dossier_folder)
    if os.path.isdir(os.path.join(dossier_folder, f)) and f.isdigit()
)

# Renommage en deux passes pour éviter les conflits (ex: 2→1 alors que 1 existe encore)
temp_names = []
for i, num in enumerate(remaining, start=1):
    old_path = os.path.join(dossier_folder, str(num))
    temp_path = os.path.join(dossier_folder, f"temp_{i}")
    os.rename(old_path, temp_path)
    temp_names.append((temp_path, os.path.join(dossier_folder, str(i))))

for temp_path, final_path in temp_names:
    os.rename(temp_path, final_path)
    print(f"Renommé : {os.path.basename(temp_path).replace('temp_', '')} → {os.path.basename(final_path)}")

print("\nSuppression et renumérotation terminées !")