import subprocess
import shlex
import os


# ---------------------------------------------------------
# CONFIGURATION — HARD-CODED ADB PATH (NO WinError 2 EVER)
# ---------------------------------------------------------
ADB = r"C:\platform-tools\adb.exe"   # <-- Update if your adb.exe is elsewhere


# ---------------------------------------------------------
# UTILITY: RUN ADB COMMAND SAFELY
# ---------------------------------------------------------
def run_adb(cmd):
    """Run an adb command with full path and return stdout."""
    full_cmd = f"\"{ADB}\" {cmd}"
    result = subprocess.run(
        shlex.split(full_cmd),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True
    )

    if result.returncode != 0:
        raise RuntimeError(
            f"ADB command failed:\n{full_cmd}\n\nError:\n{result.stderr}"
        )

    return result.stdout.strip()


# ---------------------------------------------------------
# DETECT FIRST CONNECTED DEVICE
# ---------------------------------------------------------
def get_first_device_serial():
    out = run_adb("devices")

    lines = [l for l in out.splitlines() if "\tdevice" in l]

    if not lines:
        raise RuntimeError(
            "No Meta/Quest headset detected.\n"
            "Make sure:\n"
            " - Developer Mode is enabled\n"
            " - USB cable is connected\n"
            " - You accepted the USB debugging prompt"
        )

    return lines[0].split("\t")[0]


# ---------------------------------------------------------
# GET ANDROID SYSTEM PROPERTY
# ---------------------------------------------------------
def get_prop(serial, prop):
    return run_adb(f"-s {serial} shell getprop {prop}")


# ---------------------------------------------------------
# COLLECT IDENTIFYING INFORMATION
# ---------------------------------------------------------
def collect_meta_headset_info():
    serial = get_first_device_serial()

    info = {
        "adb_serial": serial,
        "model": get_prop(serial, "ro.product.model"),
        "manufacturer": get_prop(serial, "ro.product.manufacturer"),
        "fingerprint": get_prop(serial, "ro.build.fingerprint"),
        "android_version": get_prop(serial, "ro.build.version.release"),
        "sdk_level": get_prop(serial, "ro.build.version.sdk"),
        "device": get_prop(serial, "ro.product.device"),
        "hardware": get_prop(serial, "ro.hardware"),
    }

    return info


# ---------------------------------------------------------
# MAIN EXECUTION
# ---------------------------------------------------------
if __name__ == "__main__":
    print("\n=== META HEADSET IDENTIFICATION TOOL ===\n")

    if not os.path.exists(ADB):
        print(f"ERROR: ADB not found at:\n{ADB}")
        print("Fix: Install Android Platform Tools and update the ADB path.")
        exit(1)

    try:
        info = collect_meta_headset_info()

        print("Meta / Quest Headset Identifying Information")
        print("===========================================\n")

        for key, value in info.items():
            print(f"{key}: {value}")

    except Exception as e:
        print("\nERROR:", e)
