#!/bin/bash
# ShopRenter Integration Deployment Script
# This script deploys all ShopRenter Edge Functions and sets up the database

set -e

echo "🚀 ShopRenter Integration Deployment"
echo "===================================="
echo ""

# Check if Supabase CLI is installed
if ! command -v supabase &> /dev/null; then
    echo "❌ Supabase CLI not found. Please install it first:"
    echo "   https://supabase.com/docs/guides/cli"
    exit 1
fi

echo "✅ Supabase CLI found: $(supabase --version)"
echo ""

# Check if logged in
if ! supabase projects list &> /dev/null; then
    echo "❌ Not logged in to Supabase. Please run:"
    echo "   supabase login"
    exit 1
fi

echo "✅ Logged in to Supabase"
echo ""

# Set environment variables
echo "⚙️  Setting Edge Functions secrets..."
supabase secrets set \
  SHOPRENTER_CLIENT_ID=SHOPRENTER_CLIENT_ID_PLACEHOLDER \
  SHOPRENTER_CLIENT_SECRET=SHOPRENTER_CLIENT_SECRET_PLACEHOLDER \
  INTERNAL_SYNC_SECRET=0fde967ddc5f22100909e11fc145cc04a47c849bf9dcb740f6275ab1fa173d0e \
  FRONTEND_URL=https://shopcall.ai \
  SUPABASE_URL=https://ztklqodcdjeqpsvhlpud.supabase.co \
  SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inp0a2xxb2RjZGplcXBzdmhscHVkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk3MDkzODMsImV4cCI6MjA2NTI4NTM4M30.Z4D2Ly8_VZc7SoAwDPncBo2XZQbNcps9ATu7u-tWgqY \
  SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inp0a2xxb2RjZGplcXBzdmhscHVkIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc0OTcwOTM4MywiZXhwIjoyMDY1Mjg1MzgzfQ.pcZlXh-oPk-2vYdFoMJeQ4af4thDTYelYxMYzWtyfy8

echo "✅ Secrets configured"
echo ""

# Deploy Edge Functions
echo "📦 Deploying Edge Functions..."
echo ""

FUNCTIONS=(
  "oauth-shoprenter-init"
  "oauth-shoprenter-callback"
  "webhook-shoprenter-uninstall"
  "shoprenter-products"
  "shoprenter-orders"
  "shoprenter-customers"
  "shoprenter-sync"
  "shoprenter-scheduled-sync"
)

for func in "${FUNCTIONS[@]}"; do
  echo "  Deploying $func..."
  if supabase functions deploy "$func" --no-verify-jwt; then
    echo "  ✅ $func deployed successfully"
  else
    echo "  ❌ Failed to deploy $func"
    exit 1
  fi
  echo ""
done

echo "✅ All Edge Functions deployed!"
echo ""

# Deploy database migrations
echo "🗄️  Deploying database migrations..."
if supabase db push; then
  echo "✅ Database migrations applied"
else
  echo "❌ Failed to apply database migrations"
  echo ""
  echo "You can also run the migration manually:"
  echo "  1. Go to https://supabase.com/dashboard/project/ztklqodcdjeqpsvhlpud/sql/new"
  echo "  2. Copy contents of supabase/migrations/20250129_shoprenter_scheduled_sync.sql"
  echo "  3. Paste and click 'Run'"
fi
echo ""

# Final instructions
echo "🎉 Deployment Complete!"
echo "======================"
echo ""
echo "📋 Next Steps:"
echo ""
echo "1. Configure database settings in Supabase Dashboard:"
echo "   → Project Settings → Database → Custom Postgres Configuration"
echo "   → Add:"
echo "      app.internal_sync_secret = 0fde967ddc5f22100909e11fc145cc04a47c849bf9dcb740f6275ab1fa173d0e"
echo "      app.supabase_url = https://ztklqodcdjeqpsvhlpud.supabase.co"
echo "   → Restart database"
echo ""
echo "2. When you get real ShopRenter credentials, update secrets:"
echo "   supabase secrets set SHOPRENTER_CLIENT_ID=<real_client_id>"
echo "   supabase secrets set SHOPRENTER_CLIENT_SECRET=<real_client_secret>"
echo ""
echo "3. Test the OAuth flow with a ShopRenter test store"
echo ""
echo "✅ All ShopRenter Edge Functions are now live!"
echo "✅ Scheduled background sync will run every hour"
echo "✅ Integration is ready for use!"
