#!/bin/bash
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#   OrganicAI — macOS Auto-Installer
#   Downloads, extracts, and opens chrome://extensions.
#   The path is in the clipboard — paste into Load Unpacked.
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

set -e

# NOTE: this URL is auto-updated by build/package.mjs on each release.
# Do not edit by hand — change the version once in manifest.json + repack.
DOWNLOAD_URL="https://organicai-web.pages.dev/dist/organicai-v1.0.23.zip"
INSTALL_DIR="$HOME/Library/Application Support/OrganicAI"
ZIP_PATH="/tmp/organicai-latest.zip"

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BOLD='\033[1m'
NC='\033[0m'

clear
echo ""
echo "  ╔════════════════════════════════════════════════════╗"
echo "  ║                                                    ║"
echo "  ║       OrganicAI — Auto-Installer                   ║"
echo "  ║       Autonomous social, refined.                  ║"
echo "  ║                                                    ║"
echo "  ╚════════════════════════════════════════════════════╝"
echo ""

# ─── Step 1: Download ────────────────────────────────────────
echo "  1/4  ${BOLD}Downloading the latest plugin...${NC}"
echo "       (~311 KB from organicai-web.pages.dev)"
echo ""

if ! curl -fsSL "$DOWNLOAD_URL" -o "$ZIP_PATH"; then
  echo -e "  ${RED}❌ Download failed. Check your internet connection.${NC}"
  read -n 1 -s -r -p "  Press any key to exit..."
  exit 1
fi
echo -e "       ${GREEN}✓ Downloaded${NC}"
echo ""

# ─── Step 2: Clean previous install ──────────────────────────
echo "  2/4  ${BOLD}Removing any previous install...${NC}"
rm -rf "$INSTALL_DIR" 2>/dev/null || true
mkdir -p "$INSTALL_DIR"
echo -e "       ${GREEN}✓ Clean${NC}"
echo ""

# ─── Step 3: Extract ─────────────────────────────────────────
echo "  3/4  ${BOLD}Extracting to:${NC}"
echo "       $INSTALL_DIR"
echo ""

if ! unzip -q "$ZIP_PATH" -d "$INSTALL_DIR"; then
  echo -e "  ${RED}❌ Extraction failed.${NC}"
  read -n 1 -s -r -p "  Press any key to exit..."
  exit 1
fi
rm -f "$ZIP_PATH"
echo -e "       ${GREEN}✓ Extracted${NC}"
echo ""

# ─── Step 4: Copy path + open Chrome ─────────────────────────
echo "  4/4  ${BOLD}Copying path to clipboard + opening Chrome...${NC}"

# Copy the install path to clipboard
echo -n "$INSTALL_DIR" | pbcopy

# Find Chrome (try common locations)
CHROME=""
for path in \
  "/Applications/Google Chrome.app" \
  "/Applications/Google Chrome Beta.app" \
  "/Applications/Google Chrome Canary.app" \
  "/Applications/Chromium.app" \
  "/Applications/Microsoft Edge.app" \
  "/Applications/Brave Browser.app"
do
  if [ -d "$path" ]; then
    CHROME="$path"
    break
  fi
done

if [ -n "$CHROME" ]; then
  open -a "$CHROME" "chrome://extensions/"
  echo -e "       ${GREEN}✓ $(basename "$CHROME" .app) opened${NC}"
else
  echo -e "       ${YELLOW}⚠ Chrome not found — open it manually and go to: chrome://extensions${NC}"
fi

echo ""
echo "  ╔════════════════════════════════════════════════════╗"
echo "  ║                                                    ║"
echo -e "  ║   ${GREEN}✅ Ready! Just 2 clicks left:${NC}                       ║"
echo "  ║                                                    ║"
echo "  ║   1. Toggle \"Developer mode\" (top right)           ║"
echo "  ║                                                    ║"
echo "  ║   2. Click \"Load unpacked\" → Cmd+V to paste path   ║"
echo "  ║      (the path is already in your clipboard)       ║"
echo "  ║                                                    ║"
echo "  ║   Install path (also in clipboard):                ║"
echo "  ║   $INSTALL_DIR"
echo "  ║                                                    ║"
echo "  ╚════════════════════════════════════════════════════╝"
echo ""
echo "  💡 Questions? hello@organicai.io"
echo ""
read -n 1 -s -r -p "  Press any key to close this window..."
echo ""
