#!/bin/bash
#
# Newcomer CMA - One-Click Setup
# Double-click this file to install.
#
# If macOS says "cannot be opened because it is from an unidentified developer",
# right-click the file and choose Open, then click Open again.
#

set -u

PLUGIN_URL="https://newcomer-group-cma.pages.dev/newcomer-cma.plugin"
PLUGIN_PATH="$HOME/Desktop/newcomer-cma.plugin"
LOG_FILE="$HOME/Desktop/newcomer-cma-setup.log"

# Redirect all script output to a log file so Terminal stays quiet
exec > "$LOG_FILE" 2>&1

# Move to a sane working directory
cd "$HOME"

# Give Terminal a moment, then hide it
osascript -e 'tell application "Terminal" to set miniaturized of front window to true' >/dev/null 2>&1 &

# -----------------------------------------------------------------
# 1. Welcome dialog
# -----------------------------------------------------------------
RESP=$(osascript <<'OSA'
display dialog "Welcome to the Newcomer CMA setup.

This will:

   - Download the CMA plugin to your Desktop
   - Open Claude for you
   - Show you the final clicks to finish

Takes about two minutes." with title "Newcomer CMA Setup" buttons {"Cancel", "Lets go"} default button "Lets go"
OSA
)
if [ $? -ne 0 ]; then
  exit 0
fi

# -----------------------------------------------------------------
# 2. Check Claude is installed
# -----------------------------------------------------------------
if [ ! -d "/Applications/Claude.app" ] && [ ! -d "$HOME/Applications/Claude.app" ]; then
  osascript <<'OSA'
display dialog "Claude is not installed on this Mac yet.

We will open the download page in your browser.

After you install Claude and sign in, come back and double-click this setup file again." with title "Install Claude First" buttons {"Open download page"} default button "Open download page" with icon caution
OSA
  open "https://claude.ai/download"
  exit 0
fi

# -----------------------------------------------------------------
# 3. Download the plugin
# -----------------------------------------------------------------
osascript -e 'display notification "Downloading Newcomer CMA plugin..." with title "Newcomer CMA Setup"' >/dev/null 2>&1

if ! curl -fL --connect-timeout 15 --max-time 120 -s -o "$PLUGIN_PATH" "$PLUGIN_URL"; then
  osascript <<'OSA'
display dialog "Could not download the plugin. Check your internet connection and try again.

If it keeps failing, text Steven and he will send the file directly." with title "Download Failed" buttons {"OK"} default button "OK" with icon stop
OSA
  open "$LOG_FILE"
  exit 1
fi

# Verify we got a real file (over 10 KB)
SIZE=$(stat -f%z "$PLUGIN_PATH" 2>/dev/null || echo 0)
if [ "$SIZE" -lt 10000 ]; then
  osascript <<'OSA'
display dialog "The plugin file looks broken. Try again, or text Steven for a direct copy." with title "Download Failed" buttons {"OK"} default button "OK" with icon stop
OSA
  open "$LOG_FILE"
  exit 1
fi

# Remove quarantine attribute (some Mac setups flag curl downloads)
xattr -d com.apple.quarantine "$PLUGIN_PATH" 2>/dev/null || true

# -----------------------------------------------------------------
# 4. Open Claude and show the plugin in Finder
# -----------------------------------------------------------------
open -a "Claude"
sleep 2
open -R "$PLUGIN_PATH"

# -----------------------------------------------------------------
# 5. Final instructions dialog
# -----------------------------------------------------------------
osascript <<'OSA'
display dialog "Almost done - four clicks to finish.

In Claude:

   1. Click your profile (bottom-left corner)
   2. Click Settings
   3. Click Customize
   4. Click the + button, then Create a skill, then Upload
        Pick   newcomer-cma.plugin   from your Desktop
        (it is highlighted in Finder for you)

When the skill loads, type this in Claude to test it:

        Start a CMA for 123 Main St

Stuck? Text Steven." with title "Four clicks left!" buttons {"Got it"} default button "Got it"
OSA

# -----------------------------------------------------------------
# 6. Done - close Terminal cleanly
# -----------------------------------------------------------------
osascript -e 'tell application "Terminal" to close (every window whose name contains "Setup Newcomer CMA")' >/dev/null 2>&1 &
osascript -e 'tell application "Terminal" to quit' >/dev/null 2>&1 &

exit 0
