#!/bin/bash
# ============================================================
# ChatRDX Excel Add-in — Mac Installer
# install-chatrdx-mac.sh
#
# What this does:
#   1. Creates the Office trusted manifest folder if needed
#   2. Downloads the manifest from chatrdx.radiqxledger.com
#   3. Copies it to Excel's watched WEF folder
#
# Usage: Double-click in Finder (if .command extension)
#        or: bash install-chatrdx-mac.sh
# ============================================================

set -e

# ── Config ────────────────────────────────────────────────────
MANIFEST_URL="https://chatrdx.radiqxledger.com/manifest.xml"
WEF_DIR="$HOME/Library/Containers/com.microsoft.Excel/Data/Documents/wef"
MANIFEST_FILE="$WEF_DIR/chatrdx-manifest.xml"

# ── Colors ────────────────────────────────────────────────────
CYAN='\033[0;36m'
WHITE='\033[1;37m'
GRAY='\033[0;37m'
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
RESET='\033[0m'

clear

echo ""
echo -e "${CYAN}  ╔═══════════════════════════════════════════════╗${RESET}"
echo -e "${CYAN}  ║         ChatRDX Excel Add-in                  ║${RESET}"
echo -e "${CYAN}  ║         Mac Installer · Radiqx                ║${RESET}"
echo -e "${CYAN}  ╚═══════════════════════════════════════════════╝${RESET}"
echo ""

# ── Check Excel is installed ──────────────────────────────────
echo -e "  ${YELLOW}[1/3]${RESET} Checking for Microsoft Excel..."
if [ ! -d "/Applications/Microsoft Excel.app" ]; then
    echo ""
    echo -e "  ${RED}ERROR: Microsoft Excel does not appear to be installed.${RESET}"
    echo -e "  ${GRAY}Install Excel from the Mac App Store or Microsoft 365 and run this installer again.${RESET}"
    echo ""
    read -p "  Press Enter to exit."
    exit 1
fi
echo -e "  ${GRAY}       Excel found.${RESET}"

# ── Create WEF folder ─────────────────────────────────────────
echo -e "  ${YELLOW}[2/3]${RESET} Preparing manifest folder..."

# The WEF folder only exists after Excel has been opened at least once.
# We create it if absent.
if [ ! -d "$WEF_DIR" ]; then
    mkdir -p "$WEF_DIR"
    echo -e "  ${GRAY}       Folder created: $WEF_DIR${RESET}"
else
    echo -e "  ${GRAY}       Folder exists: $WEF_DIR${RESET}"
fi

# ── Download manifest ─────────────────────────────────────────
echo -e "  ${YELLOW}[3/3]${RESET} Downloading ChatRDX manifest..."
if curl -fsSL "$MANIFEST_URL" -o "$MANIFEST_FILE"; then
    echo -e "  ${GRAY}       Manifest saved to WEF folder.${RESET}"
else
    echo ""
    echo -e "  ${RED}ERROR: Could not download the manifest.${RESET}"
    echo -e "  ${GRAY}Check your internet connection and try again.${RESET}"
    echo -e "  ${GRAY}URL: $MANIFEST_URL${RESET}"
    echo ""
    read -p "  Press Enter to exit."
    exit 1
fi

# ── Done ──────────────────────────────────────────────────────
echo ""
echo -e "  ${GREEN}Installation complete.${RESET}"
echo ""
echo -e "  ${GRAY}─────────────────────────────────────────────────────────${RESET}"
echo -e "  ${WHITE}NEXT STEP — activate ChatRDX in Excel:${RESET}"
echo ""
echo -e "  ${WHITE}1.${RESET} Open Microsoft Excel"
echo -e "  ${WHITE}2.${RESET} Click  Insert → Add-ins → My Add-ins"
echo -e "  ${WHITE}3.${RESET} You will see ${CYAN}ChatRDX${RESET} listed — click Add"
echo -e "  ${WHITE}4.${RESET} The ${CYAN}ChatRDX${RESET} tab will appear in your Excel ribbon"
echo ""
echo -e "  ${GRAY}If ChatRDX does not appear, quit and restart Excel once.${RESET}"
echo -e "  ${GRAY}─────────────────────────────────────────────────────────${RESET}"
echo ""
echo -e "  ${CYAN}Thank you for installing ChatRDX.${RESET}"
echo -e "  ${GRAY}support: info.radiqxledger.com/support${RESET}"
echo ""
read -p "  Press Enter to close."
