#!/bin/bash

# Script to initialize and run Stripe CLI webhook forwarding
# Usage: ./stripe-init-webhook.sh --site <site> --port <port>

set -e

# Default values
SITE=""
PORT="8000"

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --site)
            SITE="$2"
            shift 2
            ;;
        --port)
            PORT="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: $0 --site <site> [--port <port>]"
            echo ""
            echo "Options:"
            echo "  --site    Required. The site domain (e.g., erp.local)"
            echo "  --port    Optional. The port number (default: 8000)"
            echo ""
            echo "Example:"
            echo "  $0 --site erp.local --port 8000"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

# Check if required flag is provided
if [ -z "$SITE" ]; then
    echo "Error: --site flag is required"
    echo "Usage: $0 --site <site> [--port <port>]"
    exit 1
fi

echo "Checking Stripe CLI installation..."

# Check if Stripe CLI is installed
if ! command -v stripe &> /dev/null; then
    echo "Stripe CLI is not installed."
    read -p "Would you like to install it now? (y/n) " -n 1 -r
    echo
    
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "Installing Stripe CLI..."
        
        # Add GPG key
        echo "Adding Stripe GPG key..."
        curl -s https://packages.stripe.dev/api/security/keypair/stripe-cli-gpg/public | gpg --dearmor | sudo tee /usr/share/keyrings/stripe.gpg > /dev/null
        
        # Add repository
        echo "Adding Stripe repository..."
        echo "deb [signed-by=/usr/share/keyrings/stripe.gpg] https://packages.stripe.dev/stripe-cli-debian-local stable main" | sudo tee -a /etc/apt/sources.list.d/stripe.list
        
        # Update and install
        echo "Updating package list..."
        sudo apt update
        
        echo "Installing Stripe CLI..."
        sudo apt install stripe -y
        
        echo "Stripe CLI installed successfully!"
    else
        echo "Installation cancelled. Exiting."
        exit 1
    fi
else
    echo "Stripe CLI is already installed."
fi

# Check if Stripe CLI is authenticated
echo "Checking authentication status..."
if ! stripe config --list &> /dev/null; then
    echo "Stripe CLI is not authenticated."
    echo "Please log in to your Stripe account..."
    stripe login --interactive
else
    # Try to verify authentication by running a simple command
    if stripe config --list | grep -q "test_mode_api_key"; then
        echo "Stripe CLI is authenticated."
    else
        echo "Stripe CLI authentication may be invalid."
        echo "Please log in to your Stripe account..."
        stripe login --interactive
    fi
fi

# Start Docker containers
echo ""
echo "Starting Docker containers..."
docker compose -f docker-compose.local.yaml up -d

# Start listening for webhooks
WEBHOOK_URL="http://${SITE}:${PORT}/api/method/custom_ui.api.public.payments.stripe_webhook"
echo ""
echo "Starting Stripe webhook listener..."
echo "Forwarding to: $WEBHOOK_URL"
echo ""
echo "Press Ctrl+C to stop"
echo ""

stripe listen --forward-to "$WEBHOOK_URL"
