#!/bin/bash
clear
cd "$(dirname "$0")"

echo ""
echo "  ============================================================"
echo "   PS4 Restaurant POS | Auto Installer"
echo "  ============================================================"
echo "  [*] Platform: macOS"
echo ""

# ═══════════════════════════════════════════════════════════════
# Detect mode: dist only OR full src
# ═══════════════════════════════════════════════════════════════
echo "  [*] Detecting project type..."
echo ""

DIST_PATH=""
MODE=""

if [[ -f "ps4-pos/dist/index.html" && ! -f "ps4-pos/package.json" ]]; then
    echo "  [*] Mode: DIST ONLY (pre-built) — No build needed."
    DIST_PATH="ps4-pos/dist"
    MODE="dist"
elif [[ -f "ps4-pos/package.json" ]]; then
    echo "  [*] Mode: SOURCE — Will install and build."
    MODE="src"
elif [[ -f "dist/index.html" ]]; then
    echo "  [*] Mode: DIST ONLY (root level) — No build needed."
    DIST_PATH="dist"
    MODE="dist"
else
    echo "  [!] No project found! Make sure this file is next to:"
    echo "      ps4-pos/dist/   OR   ps4-pos/package.json"
    echo ""
    read -p "  Press Enter to exit..."
    exit 1
fi

# ═══════════════════════════════════════════════════════════════
# Node.js check (needed even for dist — for npx serve)
# ═══════════════════════════════════════════════════════════════
echo ""
echo "  [*] Checking Node.js..."
echo ""

if command -v node &>/dev/null; then
    NODE_VER=$(node -v)
    echo "  [OK] Node.js: $NODE_VER"
else
    echo "  [!] Node.js not found. Installing..."
    if command -v brew &>/dev/null; then
        brew install node
    else
        echo "  [*] Installing Homebrew + Node.js (3-6 min)..."
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
        if [[ -f "/opt/homebrew/bin/brew" ]]; then
            eval "$(/opt/homebrew/bin/brew shellenv)"
            echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
        elif [[ -f "/usr/local/bin/brew" ]]; then
            eval "$(/usr/local/bin/brew shellenv)"
        fi
        brew install node
    fi
    if ! command -v node &>/dev/null; then
        echo "  [!] Node.js install failed. Visit https://nodejs.org"
        read -p "  Press Enter to exit..."
        exit 1
    fi
    echo "  [OK] Node.js installed: $(node -v)"
fi

# ═══════════════════════════════════════════════════════════════
# SOURCE MODE — npm install + build
# ═══════════════════════════════════════════════════════════════
if [[ "$MODE" == "src" ]]; then
    echo ""
    echo "  [STEP 1/2]  npm install..."
    echo ""
    cd ps4-pos
    npm install
    if [[ $? -ne 0 ]]; then
        echo "  [!] npm install failed!"
        cd ..
        read -p "  Press Enter to exit..."
        exit 1
    fi

    echo ""
    echo "  [STEP 2/2]  Building..."
    echo ""
    npm run build
    if [[ $? -ne 0 ]]; then
        echo "  [!] Build failed!"
        cd ..
        read -p "  Press Enter to exit..."
        exit 1
    fi
    cd ..
    DIST_PATH="ps4-pos/dist"
    echo "  [OK] Build complete."
fi

# ── Create START.command ───────────────────────────────────────
cat > START.command << STARTEOF
#!/bin/bash
clear
cd "\$(dirname "\$0")"
echo ""
echo "  ============================================================"
echo "   PS4 Restaurant POS | Running..."
echo "  ============================================================"
echo "  [*] URL: http://localhost:5173"
echo "  [*] Press Ctrl+C to stop."
echo "  ============================================================"
echo ""
(sleep 2 && open "http://localhost:5173") &
npx serve ${DIST_PATH} --listen 5173 --no-clipboard
STARTEOF
chmod +x START.command

echo ""
echo "  ============================================================"
echo "   DONE! Double-click START.command to launch anytime."
echo "   URL: http://localhost:5173"
echo "   Code is compiled and hidden — src not needed."
echo "  ============================================================"
echo ""
read -p "  Launch now? (y/n): " LAUNCH
if [[ "$LAUNCH" == "y" || "$LAUNCH" == "Y" ]]; then
    bash START.command
fi
