# uf/cli.py
from .engine import UFEngine

VERSION = "1.4.1"

# List of deprecated versions (manual)
DEPRECATED_VERSIONS = [
    "1.0.0",
]

# Optional: latest known release for comparison
LATEST_VERSION = "1.4.1"

def version_status(current_version):
    if current_version in DEPRECATED_VERSIONS:
        return f"[UF Notice] You are using a deprecated version: {current_version}"
    elif current_version != LATEST_VERSION:
        return f"[UF Notice] Your version ({current_version}) is older than the latest release ({LATEST_VERSION})"
    else:
        return f"[UF] Version {current_version} is up to date"

def main():
    import sys
    import os

    args = sys.argv[1:]

    # ---------------------------
    # Version Flag
    # ---------------------------
    if "--version" in args or "-v" in args:
        print(version_status(VERSION))
        return

    # ---------------------------
    # About Flag
    # ---------------------------
    if "--about" in args:
        print("UF Language")
        print(f"Version: {VERSION}")
        print(version_status(VERSION))  # Show status here too
        print("Modes: UF, iUF, nUF, dnUF")
        print("Engine: UF Runtime")
        print("A user-friendly programming language interpreter.")
        return

    engine = UFEngine()

    # Register modes
    from .engine import UFMode, iUFMode, nUFMode, dnUFMode
    engine.register_mode("uf", UFMode)
    engine.register_mode("iuf", iUFMode)
    engine.register_mode("nuf", nUFMode)
    engine.register_mode("dnuf", dnUFMode)

    # ---------------------------
    # Select Mode
    # ---------------------------
    # Default
    selected_mode = "uf"

    # CLI argument selection
    if "--mode" in args:
        idx = args.index("--mode")
        if idx + 1 < len(args):
            mode_candidate = args[idx + 1].lower()
            if mode_candidate in ["uf", "iuf", "nuf", "dnuf"]:
                selected_mode = mode_candidate
            else:
                print(f"[UF Warning] Invalid mode '{mode_candidate}', defaulting to UF")
    elif "-m" in args:
        idx = args.index("-m")
        if idx + 1 < len(args):
            mode_candidate = args[idx + 1].lower()
            if mode_candidate in ["uf", "iuf", "nuf", "dnuf"]:
                selected_mode = mode_candidate

    # Interactive selection if no mode passed
    if selected_mode == "uf" and not ("--mode" in args or "-m" in args):
        print("Select mode (UF / iUF / nUF / dnUF): ", end="")
        user_input = input().strip().lower()
        if user_input in ["uf", "iuf", "nuf", "dnuf"]:
            selected_mode = user_input
        else:
            print("[UF Notice] Invalid mode selected, defaulting to UF")

    engine.select_mode(selected_mode)

    # ---------------------------
    # uf run  -> auto run main.uf
    # ---------------------------
    if len(args) > 0 and args[0].lower() == "run":
        main_file = os.path.join(os.getcwd(), "main.uf")

        if not os.path.isfile(main_file):
            print("[UF Error E404]")
            print("main.uf not found in current directory.")
            print(f"Expected path: {main_file}")
            return

        print(f"Running {main_file}...\n")
        engine.run_file(main_file)
        return

    # ---------------------------
    # uf file.uf  -> run file
    # ---------------------------
    if len(args) > 0:
        filename = args[0]

        if not os.path.isfile(filename):
            print(f"[UF Error E404] File not found: {filename}")
            return

        if not filename.endswith(".uf"):
            print("[UF Warning] Running non-.uf file...")

        print(f"Running {filename}...\n")
        engine.run_file(filename)
        return

    # ---------------------------
    # No args -> REPL
    # ---------------------------
    engine.run_repl()


if __name__ == "__main__":
    main()