# Cek kesiapan laptop peserta LKS AI 2026 (Cabdin Wilayah Kediri).
# Jalankan:  python cek-laptop-lks-ai.py
# Mengecek versi Python, library yang diizinkan, library terlarang, Jupyter, dan koneksi internet.
# Skrip ini hanya MEMBACA, tidak menginstal / menghapus apa pun.
import importlib
import importlib.util
import shutil
import socket
import subprocess
import sys

OK, GAGAL, PERINGATAN = "[ OK ]", "[GAGAL]", "[ CEK ]"
masalah = 0


def baris(status, teks):
    print(f"{status} {teks}")


print("=" * 60)
print(" CEK LAPTOP LKS AI 2026")
print("=" * 60)

# 1. Python
v = sys.version_info
if v >= (3, 10):
    baris(OK, f"Python {v.major}.{v.minor}.{v.micro}  ({sys.executable})")
else:
    masalah += 1
    baris(GAGAL, f"Python {v.major}.{v.minor} terlalu lama. Pasang Python 3.11 atau 3.12 dari python.org")

# 2. Library yang diizinkan
print("\nLibrary yang diizinkan:")
for nama in ["numpy", "pandas", "matplotlib", "seaborn"]:
    try:
        m = importlib.import_module(nama)
        baris(OK, f"{nama} {getattr(m, '__version__', '')}")
    except Exception:
        masalah += 1
        baris(GAGAL, f"{nama} belum terpasang  ->  pip install {nama}")

try:
    import tkinter

    r = tkinter.Tk()
    r.withdraw()
    r.destroy()
    baris(OK, f"tkinter (Tk {tkinter.TkVersion})")
except Exception as e:
    masalah += 1
    baris(GAGAL, f"tkinter tidak bisa dipakai ({e}). Instal ulang Python, centang 'tcl/tk and IDLE'")

# 3. Library terlarang
print("\nLibrary yang TIDAK boleh ada:")
terlarang = ["sklearn", "scipy", "xgboost", "lightgbm", "tensorflow", "keras", "torch", "statsmodels", "plotly"]
ada = []
for nama in terlarang:
    try:
        if importlib.util.find_spec(nama) is not None:
            ada.append(nama)
    except Exception:
        pass
if ada:
    masalah += 1
    pip_nama = {"sklearn": "scikit-learn", "torch": "torch"}
    baris(GAGAL, "Terpasang: " + ", ".join(ada))
    print("        Hapus dengan:  pip uninstall -y " + " ".join(pip_nama.get(n, n) for n in ada))
else:
    baris(OK, "Tidak ada (scikit-learn, scipy, tensorflow, pytorch, dll.)")

# 4. Jupyter Notebook
print("\nAlat kerja:")
try:
    importlib.import_module("notebook")
    baris(OK, "Jupyter Notebook terpasang  (jalankan: jupyter notebook)")
except Exception:
    masalah += 1
    baris(GAGAL, "Jupyter Notebook belum terpasang  ->  pip install notebook ipykernel")

code = shutil.which("code")
if code:
    try:
        ext = subprocess.run([code, "--list-extensions"], capture_output=True, text=True, timeout=30, shell=sys.platform == "win32").stdout.lower()
    except Exception:
        ext = ""
    baris(OK, "Visual Studio Code terpasang")
    for e, label in [("ms-python.python", "ekstensi Python"), ("ms-toolsai.jupyter", "ekstensi Jupyter")]:
        baris(OK if e in ext else PERINGATAN, f"VS Code {label}" + ("" if e in ext else " belum terlihat (pasang selagi online)"))
    ai = [x for x in ext.split() if any(k in x for k in ["copilot", "codeium", "tabnine", "continue", "cody", "supermaven", "blackbox"])]
    if ai:
        masalah += 1
        baris(GAGAL, "Ekstensi AI di VS Code harus dihapus/dinonaktifkan: " + ", ".join(ai))
else:
    baris(PERINGATAN, "Perintah 'code' tidak ditemukan. Abaikan bila VS Code memang tidak dipakai / dipasang tanpa PATH")

# 5. Internet
print("\nKoneksi:")
try:
    socket.create_connection(("8.8.8.8", 53), timeout=3).close()
    baris(PERINGATAN, "Internet MASIH tersambung. Saat lomba Wi-Fi harus dimatikan")
except OSError:
    baris(OK, "Tidak ada internet (sesuai aturan lomba)")

print("\n" + "=" * 60)
if masalah:
    print(f" ADA {masalah} MASALAH. Perbaiki baris [GAGAL] di atas, lalu jalankan ulang.")
else:
    print(" LAPTOP SIAP. Semua pengecekan lolos.")
print("=" * 60)
if sys.platform == "win32":
    input("\nTekan Enter untuk menutup...")
