"""
Auto File Organizer
by Christian Sesay - Sierra Leone

Sorts every file in a target folder into subfolders by file type
(Documents, Images, Spreadsheets, Code, Archives, etc).
Built for the Google IT Automation with Python Certificate.
"""

import os
import shutil

FOLDER_MAP = {
    "Documents": [".pdf", ".docx", ".doc", ".txt"],
    "Spreadsheets": [".xlsx", ".xls", ".csv"],
    "Images": [".png", ".jpg", ".jpeg", ".gif", ".svg"],
    "Code": [".py", ".js", ".html", ".css", ".java", ".cpp"],
    "Archives": [".zip", ".rar", ".tar", ".gz"],
}


def get_category(extension):
    for category, extensions in FOLDER_MAP.items():
        if extension.lower() in extensions:
            return category
    return "Other"


def organize_folder(target_path):
    if not os.path.isdir(target_path):
        print(f"'{target_path}' is not a valid folder.")
        return

    moved_count = 0
    print(f"\nScanning '{target_path}'...\n")

    for filename in os.listdir(target_path):
        file_path = os.path.join(target_path, filename)

        if os.path.isdir(file_path):
            continue

        _, extension = os.path.splitext(filename)
        category = get_category(extension)
        category_path = os.path.join(target_path, category)

        os.makedirs(category_path, exist_ok=True)
        shutil.move(file_path, os.path.join(category_path, filename))
        print(f"Moved: {filename} → {category}/")
        moved_count += 1

    print(f"\nDone. {moved_count} file(s) organized into {len(FOLDER_MAP) + 1} possible categories.")


if __name__ == "__main__":
    print("=" * 60)
    print("AUTO FILE ORGANIZER")
    print("by Christian Sesay - Sierra Leone")
    print("=" * 60)
    folder = input("\nEnter the full path of the folder to organize → ").strip()
    organize_folder(folder)
