#!/usr/bin/env python3
"""One-time setup. No admin rights needed."""
import os
import sys
import subprocess
import shutil

BASE = os.path.dirname(os.path.abspath(__file__))
VENV = os.path.join(BASE, ".venv")
REQS = os.path.join(BASE, "backend", "requirements.txt")

def find_python():
    for name in ["python3", "python"]:
        path = shutil.which(name)
        if path:
            return path
    print("Python not found. Install from python.org or Microsoft Store.")
    sys.exit(1)

PYTHON = find_python()

print("Creating virtual environment...")
subprocess.check_call([PYTHON, "-m", "venv", VENV])

VENV_PY = os.path.join(VENV, "Scripts" if sys.platform == "win32" else "bin", "python")
print("Installing dependencies...")
subprocess.check_call([VENV_PY, "-m", "pip", "install", "-r", REQS])

print("Creating launchers...")

if sys.platform == "win32":
    # .vbs — no console window
    vbs_path = os.path.join(BASE, "Start Dealboard.vbs")
    with open(vbs_path, "w") as f:
        f.write('Set WshShell = CreateObject("WScript.Shell")\n')
        f.write(f'WshShell.Run "\"{VENV_PY}\" \"{os.path.join(BASE, "start.py")}\"", 0, False\n')
    # .bat — visible console fallback
    bat_path = os.path.join(BASE, "Start Dealboard.bat")
    with open(bat_path, "w") as f:
        f.write(f'@echo off\n"{VENV_PY}" "{os.path.join(BASE, "start.py")}"\n')
    print(f"Done. Double-click 'Start Dealboard.vbs'.")
else:
    # macOS .command
    cmd_path = os.path.join(BASE, "Launch Dealboard.command")
    with open(cmd_path, "w") as f:
        f.write(f'#!/bin/bash\ncd "{BASE}" && "{VENV_PY}" "{os.path.join(BASE, "start.py")}"\n')
    os.chmod(cmd_path, 0o755)
    print(f"Done. Double-click 'Launch Dealboard.command'.")

print("Setup complete.")
