#!/bin/bash

# Deploy WooCommerce Integration Functions
# This script deploys all Supabase Edge Functions required for WooCommerce integration

set -e

echo "======================================"
echo "Deploying WooCommerce Integration"
echo "======================================"
echo ""

# Check if supabase CLI is installed
if ! command -v supabase &> /dev/null; then
    echo "Error: Supabase CLI is not installed"
    echo "Install it with: npm install -g supabase"
    exit 1
fi

# Check if we're logged in to Supabase
if ! supabase projects list &> /dev/null; then
    echo "Error: Not logged in to Supabase"
    echo "Login with: supabase login"
    exit 1
fi

# Deploy OAuth function
echo "1. Deploying oauth-woocommerce function..."
supabase functions deploy oauth-woocommerce --no-verify-jwt

if [ $? -eq 0 ]; then
    echo "✓ oauth-woocommerce deployed successfully"
else
    echo "✗ Failed to deploy oauth-woocommerce"
    exit 1
fi

echo ""
echo "======================================"
echo "Deployment Summary"
echo "======================================"
echo "✓ oauth-woocommerce - OAuth 1.0a authentication flow"
echo ""
echo "WooCommerce integration deployed successfully!"
echo ""
echo "Next steps:"
echo "1. Verify environment variables are set:"
echo "   - FRONTEND_URL"
echo "   - SUPABASE_URL"
echo "   - SUPABASE_ANON_KEY"
echo "   - SUPABASE_SERVICE_ROLE_KEY"
echo ""
echo "2. Test the OAuth flow:"
echo "   - Go to https://shopcall.ai/webshops"
echo "   - Click 'Connect Webshop'"
echo "   - Select 'WooCommerce'"
echo "   - Enter a valid WooCommerce store URL"
echo ""
echo "3. Monitor logs:"
echo "   supabase functions logs oauth-woocommerce"
echo ""
