#!/usr/bin/env bash
set -euo pipefail

# Static Site Download - FTP download helper script
# Usage: download.sh <project-name> <target-dir> [--config path]

# ─── Argument Parsing ───────────────────────────────────────────────

PROJECT_NAME=""
TARGET_DIR=""
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/static-site-deploy.yml"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --config)
      CONFIG_FILE="$2"
      shift 2
      ;;
    *)
      if [[ -z "$PROJECT_NAME" ]]; then
        PROJECT_NAME="$1"
      elif [[ -z "$TARGET_DIR" ]]; then
        TARGET_DIR="$1"
      else
        echo "Error: unexpected argument '$1'" >&2
        exit 1
      fi
      shift
      ;;
  esac
done

if [[ -z "$PROJECT_NAME" || -z "$TARGET_DIR" ]]; then
  echo "Usage: download.sh <project-name> <target-dir> [--config path]" >&2
  exit 1
fi

# Read bot_id from environment variable
BOT_ID="${ASSISTANT_ID:-}"
if [[ -z "$BOT_ID" ]]; then
  echo "Error: ASSISTANT_ID environment variable is not set" >&2
  exit 1
fi

# ─── Config Parsing ─────────────────────────────────────────────────

if [[ ! -f "$CONFIG_FILE" ]]; then
  echo "Error: config file '$CONFIG_FILE' not found" >&2
  exit 1
fi

# Simple YAML parser (no external dependencies)
parse_yaml() {
  local key="$1"
  grep "^${key}:" "$CONFIG_FILE" | sed "s/^${key}:[[:space:]]*//" | sed 's/[[:space:]]*#.*//' | sed 's/[[:space:]]*$//' | sed 's/^[\"'\'']\(.*\)[\"'\'']$/\1/'
}

HOST=$(parse_yaml "host")
FTP_USER=$(parse_yaml "ftp_user")
FTP_PASS=$(parse_yaml "ftp_pass")
FTP_PORT=$(parse_yaml "ftp_port")
USE_FTPS=$(parse_yaml "use_ftps")
WEB_ROOT=$(parse_yaml "web_root")
DOMAIN=$(parse_yaml "domain")

# Defaults
FTP_PORT="${FTP_PORT:-21}"
USE_FTPS="${USE_FTPS:-true}"

# Validate required fields
for field in HOST FTP_USER FTP_PASS WEB_ROOT DOMAIN; do
  if [[ -z "${!field}" ]]; then
    echo "Error: missing required config field: $(echo "$field" | tr '[:upper:]' '[:lower:]')" >&2
    exit 1
  fi
done

REMOTE_PATH="${WEB_ROOT}/${BOT_ID}/${PROJECT_NAME}"
SITE_URL="https://${DOMAIN}/${BOT_ID}/${PROJECT_NAME}/"

# ─── Summary ────────────────────────────────────────────────────────

echo "=== Static Site Download ==="
echo "Bot ID:      $BOT_ID"
echo "Project:     $PROJECT_NAME"
echo "Remote:      ftp://${FTP_USER}@${HOST}:${FTP_PORT}${REMOTE_PATH}/"
echo "Target:      $TARGET_DIR"
echo "FTPS:        $USE_FTPS"
echo ""

# Create target directory
mkdir -p "$TARGET_DIR"

# ─── Download ───────────────────────────────────────────────────────

echo "Downloading with curl..."

curl_ssl_flag=""
if [[ "$USE_FTPS" == "true" ]]; then
  curl_ssl_flag="--ssl-reqd"
fi

ftp_base="ftp://${FTP_USER}:${FTP_PASS}@${HOST}:${FTP_PORT}"

# List all files
echo "Listing remote files..."
file_list=$(curl -s --list-only $curl_ssl_flag "${ftp_base}${REMOTE_PATH}/" 2>/dev/null || echo "")

if [[ -z "$file_list" ]]; then
  echo "Error: Could not list remote directory or directory is empty" >&2
  exit 1
fi

downloaded=0
failed=0

# Download each file
while IFS= read -r file; do
  # Skip . and ..
  if [[ "$file" == "." || "$file" == ".." ]]; then
    continue
  fi

  remote_url="${ftp_base}${REMOTE_PATH}/${file}"
  local_file="${TARGET_DIR}/${file}"

  if curl -s -o "$local_file" $curl_ssl_flag "$remote_url" 2>/dev/null; then
    echo "  OK: $file"
    downloaded=$((downloaded + 1))
  else
    echo "  FAIL: $file" >&2
    failed=$((failed + 1))
  fi
done <<< "$file_list"

echo ""
echo "Downloaded: $downloaded file(s)"
if [[ $failed -gt 0 ]]; then
  echo "Warning: $failed file(s) failed to download" >&2
fi

echo ""

# ─── Summary ────────────────────────────────────────────────────────

FILE_COUNT=$(find "$TARGET_DIR" -type f 2>/dev/null | wc -l | tr -d ' ')
TOTAL_SIZE=$(du -sh "$TARGET_DIR" 2>/dev/null | cut -f1)

echo "=== Download Complete ==="
echo "Target:      $TARGET_DIR ($FILE_COUNT files, $TOTAL_SIZE)"
echo "Source URL:  $SITE_URL"
