#!/bin/bash

#
# Reonomy Lead Scraper
#
# A simple wrapper script to run the Reonomy lead scraper.
#
# Usage:
#   ./scrape-reonomy.sh [options]
#
# Options:
#   -h, --help           Show this help message
#   -l, --location LOC   Search location (default: "New York, NY")
#   -s, --sheet ID       Google Sheet ID (optional, creates new sheet if not provided)
#   -H, --headless       Run in headless mode (no browser window)
#   --no-headless        Run with visible browser
#   --1password          Fetch credentials from 1Password
#

set -e

# Default values
LOCATION="${REONOMY_LOCATION:-New York, NY}"
SHEET_ID="${REONOMY_SHEET_ID:-}"
HEADLESS="${HEADLESS:-false}"
USE_1PASSWORD=false

# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Help function
show_help() {
  grep '^#' "$0" | sed 's/^# //; s/^#//; 1d' | sed '2d'
}

# Parse arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    -h|--help)
      show_help
      exit 0
      ;;
    -l|--location)
      LOCATION="$2"
      shift 2
      ;;
    -s|--sheet)
      SHEET_ID="$2"
      shift 2
      ;;
    -H|--headless)
      HEADLESS=true
      shift
      ;;
    --no-headless)
      HEADLESS=false
      shift
      ;;
    --1password)
      USE_1PASSWORD=true
      shift
      ;;
    *)
      echo "❌ Unknown option: $1"
      echo "   Use --help for usage information"
      exit 1
      ;;
  esac
done

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

log_info() {
  echo -e "${BLUE}ℹ️  $1${NC}"
}

log_success() {
  echo -e "${GREEN}✅ $1${NC}"
}

log_warning() {
  echo -e "${YELLOW}⚠️  $1${NC}"
}

log_error() {
  echo -e "${RED}❌ $1${NC}"
}

# Check prerequisites
check_prerequisites() {
  log_info "Checking prerequisites..."

  # Check if Node.js is installed
  if ! command -v node &> /dev/null; then
    log_error "Node.js is not installed"
    log_info "Install it from: https://nodejs.org/"
    exit 1
  fi

  # Check if npm packages are installed
  if [ ! -d "$SCRIPT_DIR/node_modules" ]; then
    log_warning "Node modules not found, installing..."
    cd "$SCRIPT_DIR" && npm install
  fi

  # Check if gog is installed
  if ! command -v gog &> /dev/null; then
    log_error "gog CLI is not installed"
    log_info "Install it from: https://github.com/stripe/gog"
    log_warning "The scraper will save leads to JSON file instead of Google Sheets"
  else
    # Check if gog is authenticated
    if ! gog auth status &> /dev/null; then
      log_warning "gog CLI is not authenticated"
      log_info "Run: gog auth login"
      log_warning "The scraper will save leads to JSON file instead of Google Sheets"
    fi
  fi

  # Check if 1Password CLI is available when requested
  if [ "$USE_1PASSWORD" = true ]; then
    if ! command -v op &> /dev/null; then
      log_error "1Password CLI (op) is not installed"
      log_info "Install it from: https://developer.1password.com/docs/cli/"
      exit 1
    fi
  fi

  log_success "All prerequisites met"
}

# Get credentials
get_credentials() {
  if [ "$USE_1PASSWORD" = true ]; then
    log_info "Fetching credentials from 1Password..."
    # Assumes you have a Reonomy item in 1Password with email and password fields
    # You may need to adjust the item name and field names
    REONOMY_EMAIL=$(op item get "Reonomy" --field email 2>/dev/null || echo "")
    REONOMY_PASSWORD=$(op item get "Reonomy" --field password 2>/dev/null || echo "")

    if [ -z "$REONOMY_EMAIL" ] || [ -z "$REONOMY_PASSWORD" ]; then
      log_error "Could not fetch credentials from 1Password"
      log_info "Please create a 1Password item named 'Reonomy' with 'email' and 'password' fields"
      exit 1
    fi

    log_success "Credentials fetched from 1Password"
  else
    # Use environment variables or prompt
    if [ -z "$REONOMY_EMAIL" ]; then
      read -p "📧 Enter Reonomy email: " REONOMY_EMAIL
    fi

    if [ -z "$REONOMY_PASSWORD" ]; then
      read -s -p "🔑 Enter Reonomy password: " REONOMY_PASSWORD
      echo
    fi
  fi

  export REONOMY_EMAIL
  export REONOMY_PASSWORD
}

# Main execution
main() {
  echo
  echo "🏗️  Reonomy Lead Scraper"
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo

  check_prerequisites
  get_credentials

  # Set additional environment variables
  export REONOMY_LOCATION="$LOCATION"
  export REONOMY_SHEET_ID="$SHEET_ID"
  export HEADLESS="$HEADLESS"

  log_info "Configuration:"
  log_info "  Location:   $LOCATION"
  log_info "  Headless:   $HEADLESS"
  log_info "  Sheet ID:   ${SHEET_ID:-[Creating new sheet]}"
  echo

  # Run the scraper
  log_info "Starting scraper..."
  echo

  cd "$SCRIPT_DIR"

  if [ "$HEADLESS" = true ]; then
    node reonomy-scraper.js
  else
    node reonomy-scraper.js
  fi

  exit_code=$?

  echo
  if [ $exit_code -eq 0 ]; then
    log_success "Scraping completed successfully!"
  else
    log_error "Scraping failed with exit code: $exit_code"
  fi

  exit $exit_code
}

# Run main function
main "$@"
