#!/bin/bash
# refresh-refreshedtech-catalog.sh
# Downloads Refreshed Tech vendor list from shared Google Sheet, saves it to the
# sync-adapter directory so the Monday Shopify metafield sync picks it up, and
# triggers a catalog search reload (which pulls directly from the sheet URL).
#
# Sheet URL is intentionally embedded here so this script can run standalone
# from cron without depending on the app being up. The app-side catalog reload
# also uses the URL in products/vendor-uploads/vendors.json — keep both in sync.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

# Load .env for MKL_PRODUCTS_PORT only
if [ -f "$PROJECT_DIR/.env" ]; then
    while IFS='=' read -r key value; do
        [[ -z "$key" || "$key" =~ ^[[:space:]]*# ]] && continue
        key=$(echo "$key" | xargs)
        case "$key" in
            MKL_PRODUCTS_PORT)
                value=$(echo "$value" | xargs)
                export "$key=$value"
                ;;
        esac
    done < "$PROJECT_DIR/.env"
fi

PRODUCTS_PORT="${MKL_PRODUCTS_PORT:-3002}"
SHEET_ID="1kmIlLxCOpWKOVIyqLyxC3IHpyF5ovY4_OVGSmOXU8yU"
SHEET_GID="0"
SHEET_URL="https://docs.google.com/spreadsheets/d/${SHEET_ID}/export?format=xlsx&gid=${SHEET_GID}"
ADAPTER_DIR="/var/www/refreshed-tech"
ADAPTER_FILE="$ADAPTER_DIR/RT Chromebooks.xlsx"

WORK_DIR="/tmp/rt-refresh-$$"
mkdir -p "$WORK_DIR"

cleanup() {
    rm -rf "$WORK_DIR"
}
trap cleanup EXIT

echo "[RT Refresh] $(date '+%Y-%m-%d %H:%M:%S') Starting Refreshed Tech catalog refresh"

# Download sheet as xlsx
TMP_FILE="$WORK_DIR/rt-chromebooks.xlsx"
echo "[RT Refresh] Downloading Google Sheet..."
if ! curl -sSfL --max-time 60 -o "$TMP_FILE" "$SHEET_URL"; then
    echo "[RT Refresh] ERROR: Failed to download sheet"
    exit 1
fi

# Basic sanity check — real xlsx files start with PK (zip magic)
if ! head -c 2 "$TMP_FILE" | grep -q "PK"; then
    echo "[RT Refresh] ERROR: Downloaded file is not a valid xlsx (first bytes: $(head -c 4 "$TMP_FILE" | xxd))"
    exit 1
fi

NEW_SIZE=$(stat -c %s "$TMP_FILE" 2>/dev/null || stat -f %z "$TMP_FILE")
echo "[RT Refresh] Downloaded $(( NEW_SIZE / 1024 ))KB"

# Compare with existing file — skip if identical (compare uncompressed content,
# since xlsx timestamps change the byte-for-byte hash each download)
mkdir -p "$ADAPTER_DIR"
SHOULD_RELOAD=1
if [ -f "$ADAPTER_FILE" ]; then
    OLD_CONTENT_HASH=$(unzip -p "$ADAPTER_FILE" "xl/worksheets/sheet1.xml" 2>/dev/null | md5sum | cut -d' ' -f1 || echo "")
    NEW_CONTENT_HASH=$(unzip -p "$TMP_FILE" "xl/worksheets/sheet1.xml" 2>/dev/null | md5sum | cut -d' ' -f1 || echo "")
    if [ -n "$OLD_CONTENT_HASH" ] && [ "$OLD_CONTENT_HASH" = "$NEW_CONTENT_HASH" ]; then
        echo "[RT Refresh] Sheet content unchanged — skipping reload"
        SHOULD_RELOAD=0
    fi
fi

# Always refresh the file (mtime update signals freshness to sync adapter)
cp "$TMP_FILE" "$ADAPTER_FILE"
echo "[RT Refresh] Updated $ADAPTER_FILE"

if [ "$SHOULD_RELOAD" = "0" ]; then
    echo "[RT Refresh] $(date '+%Y-%m-%d %H:%M:%S') Done (no reload needed)"
    exit 0
fi

# Trigger catalog reload on products server (pulls directly from google_sheet_url)
echo "[RT Refresh] Triggering catalog reload..."
RELOAD_RESPONSE=$(curl -sf --max-time 120 "http://localhost:$PRODUCTS_PORT/refreshedtech-reload-catalog" 2>&1 || true)
if echo "$RELOAD_RESPONSE" | grep -qi "reloaded\|success"; then
    echo "[RT Refresh] Catalog reload triggered successfully"
else
    echo "[RT Refresh] Warning: Reload response: $RELOAD_RESPONSE"
    echo "[RT Refresh] File updated but reload may need manual trigger"
fi

echo "[RT Refresh] $(date '+%Y-%m-%d %H:%M:%S') Done"
