#!/bin/bash

# Script to update URLs in the OpenAPI specification file
# This is useful for switching the docs/mock server URL between local development and production
# Note: All references to DOCS_DEPLOY_URL throughout the file are toggled; the API server URL remains unchanged
#
# Usage:
#   # In git repo:
#   ./scripts/update-urls.sh --local|--production
#   
#   # In npm package:
#   ./update-urls.sh --local|--production
#
# Flags:
#   --local       Switch docs/mock server URL to local development (http://localhost:3000/v2)
#   --production  Switch docs/mock server URL to production deployment URL
#
# Environment variables (optional, with defaults):
#   LOCAL_URL - Local development URL (default: http://localhost:3000/v2)
#   DOCS_DEPLOY_URL - Docs/mock deployment URL (default: https://alpha.lunchmoney.dev/v2)
#
# Examples:
#   # Switch to production URL (using defaults)
#   ./update-urls.sh --production
#
#   # Switch to production URL (with custom URL)
#   DOCS_DEPLOY_URL=https://alpha.lunchmoney.dev/v2 ./update-urls.sh --production
#
#   # Switch to local URL
#   ./update-urls.sh --local
#
#   # Switch to local URL (with custom local URL)
#   LOCAL_URL=http://localhost:4000/v2 ./update-urls.sh --local

set -e

# Get the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Determine if we're in the git repo or npm package
# In git repo: script is in scripts/ subdirectory, spec is in v2/spec/
# In npm package: script is at root, spec is at root
if [ -f "$SCRIPT_DIR/../v2/spec/lunch-money-api-v2.yaml" ]; then
  # In the git repo (script is in scripts/ subdirectory)
  REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
  SPEC_FILE="$REPO_ROOT/v2/spec/lunch-money-api-v2.yaml"
elif [ -f "$SCRIPT_DIR/lunch-money-api-v2.yaml" ]; then
  # In the npm package (script is at root, spec is in same directory)
  REPO_ROOT="$SCRIPT_DIR"
  SPEC_FILE="$REPO_ROOT/lunch-money-api-v2.yaml"
else
  echo "Error: Could not find lunch-money-api-v2.yaml"
  echo "  Looked in: $SCRIPT_DIR/v2/spec/"
  echo "  Looked in: $SCRIPT_DIR/"
  exit 1
fi

# Load environment variables from .env file if it exists and variables are not already set
if [ -f "$REPO_ROOT/.env" ]; then
  export $(grep -v '^#' "$REPO_ROOT/.env" | xargs)
fi

# Set default values for the URLs if not provided
LOCAL_URL=${LOCAL_URL:-"http://localhost:3000/v2"}
DOCS_DEPLOY_URL=${DOCS_DEPLOY_URL:-"https://alpha.lunchmoney.dev/v2"}

# Function to show help and exit
show_help() {
  echo "Usage: $0 --local|--production"
  echo ""
  echo "Flags (required):"
  echo "  --local       Switch docs/mock server URL to local development"
  echo "  --production  Switch docs/mock server URL to production deployment"
  echo ""
  echo "Environment variables (optional, with defaults):"
  echo "  LOCAL_URL       Local development URL (default: http://localhost:3000/v2)"
  echo "  DOCS_DEPLOY_URL Docs/mock deployment URL (default: https://alpha.lunchmoney.dev/v2)"
  echo ""
  echo "Examples:"
  echo "  # Switch to production URL (using defaults)"
  echo "  $0 --production"
  echo ""
  echo "  # Switch to production URL (with custom URL)"
  echo "  DOCS_DEPLOY_URL=https://alpha.lunchmoney.dev/v2 $0 --production"
  echo ""
  echo "  # Switch to local URL"
  echo "  $0 --local"
  echo ""
  echo "  # Switch to local URL (with custom local URL)"
  echo "  LOCAL_URL=http://localhost:4000/v2 $0 --local"
  echo ""
  echo "Note: Environment variables can also be set in a .env file in the repository root."
  echo "Note: All references to DOCS_DEPLOY_URL throughout the file are updated; the API server URL remains unchanged."
  exit 1
}

# Function to replace all occurrences of the docs/mock server URL in the spec file
replace_docs_url() {
  local from_url=$1
  local to_url=$2

  if [ ! -f "$SPEC_FILE" ]; then
    echo "Error: Spec file not found at $SPEC_FILE"
    exit 1
  fi

  echo "Updating all references to docs/mock server URL in $SPEC_FILE..."
  echo "  From: $from_url"
  echo "  To: $to_url"

  # Replace all occurrences of the URL throughout the entire file
  # Using sed with proper escaping for URLs
  # Escape special regex characters in from_url
  escaped_from_url=$(echo "$from_url" | sed 's/[[\.*^$()+?{|]/\\&/g')
  # Use a temporary file for cross-platform compatibility
  sed "s|$escaped_from_url|$to_url|g" "$SPEC_FILE" > "$SPEC_FILE.tmp" && mv "$SPEC_FILE.tmp" "$SPEC_FILE"

  echo "✓ URLs updated successfully"
}

# Check for required flag
if [ "$1" == "--local" ]; then
  echo "Switching docs/mock server URL to local development..."
  echo "  Using LOCAL_URL: $LOCAL_URL"
  replace_docs_url "$DOCS_DEPLOY_URL" "$LOCAL_URL"
elif [ "$1" == "--production" ]; then
  echo "Switching docs/mock server URL to production..."
  echo "  Using DOCS_DEPLOY_URL: $DOCS_DEPLOY_URL"
  replace_docs_url "$LOCAL_URL" "$DOCS_DEPLOY_URL"
else
  echo "Error: A flag is required (--local or --production)"
  echo ""
  show_help
fi

