#!/bin/bash

# Create Stripe Prices for GBP Monthly/Yearly (for testing)
# This creates the minimum prices needed to test the checkout flow

echo "Creating Stripe prices for testing..."
echo ""

# Pricing data (GBP base prices)
# Tier 1: £179/month
# Tier 2: £209/month (most popular)
# Tier 3: £249/month
# Tier 4: £349/month
# Tier 5: £449/month

# Get product IDs
echo "Fetching product IDs..."
PRODUCTS=$(stripe products list --limit=10 --format=json)

PRODUCT_TIER1=$(echo $PRODUCTS | grep -o '"id": "prod_[^"]*"[^}]*"tier_id": "tier1"' | grep -o 'prod_[^"]*' | head -1)
PRODUCT_TIER2=$(echo $PRODUCTS | grep -o '"id": "prod_[^"]*"[^}]*"tier_id": "tier2"' | grep -o 'prod_[^"]*' | head -1)
PRODUCT_TIER3=$(echo $PRODUCTS | grep -o '"id": "prod_[^"]*"[^}]*"tier_id": "tier3"' | grep -o 'prod_[^"]*' | head -1)
PRODUCT_TIER4=$(echo $PRODUCTS | grep -o '"id": "prod_[^"]*"[^}]*"tier_id": "tier4"' | grep -o 'prod_[^"]*' | head -1)
PRODUCT_TIER5=$(echo $PRODUCTS | grep -o '"id": "prod_[^"]*"[^}]*"tier_id": "tier5"' | grep -o 'prod_[^"]*' | head -1)

if [ -z "$PRODUCT_TIER1" ] || [ -z "$PRODUCT_TIER2" ]; then
  echo "❌ Error: Products not found. Run create-stripe-products.sh first."
  exit 1
fi

echo "Found products:"
echo "  Tier 1: $PRODUCT_TIER1"
echo "  Tier 2: $PRODUCT_TIER2"
echo "  Tier 3: $PRODUCT_TIER3"
echo "  Tier 4: $PRODUCT_TIER4"
echo "  Tier 5: $PRODUCT_TIER5"
echo ""

# Create GBP monthly prices for all tiers
echo "Creating GBP Monthly prices..."

stripe prices create \
  -d product=$PRODUCT_TIER1 \
  -d currency=gbp \
  -d unit_amount=17900 \
  -d 'recurring[interval]'=month \
  -d 'metadata[tier_id]'=tier1 \
  -d 'metadata[billing_period]'=monthly
echo "✅ Tier 1 Monthly GBP: £179/month"

stripe prices create \
  -d product=$PRODUCT_TIER2 \
  -d currency=gbp \
  -d unit_amount=20900 \
  -d 'recurring[interval]'=month \
  -d 'metadata[tier_id]'=tier2 \
  -d 'metadata[billing_period]'=monthly
echo "✅ Tier 2 Monthly GBP: £209/month"

stripe prices create \
  -d product=$PRODUCT_TIER3 \
  -d currency=gbp \
  -d unit_amount=24900 \
  -d 'recurring[interval]'=month \
  -d 'metadata[tier_id]'=tier3 \
  -d 'metadata[billing_period]'=monthly
echo "✅ Tier 3 Monthly GBP: £249/month"

stripe prices create \
  -d product=$PRODUCT_TIER4 \
  -d currency=gbp \
  -d unit_amount=34900 \
  -d 'recurring[interval]'=month \
  -d 'metadata[tier_id]'=tier4 \
  -d 'metadata[billing_period]'=monthly
echo "✅ Tier 4 Monthly GBP: £349/month"

stripe prices create \
  -d product=$PRODUCT_TIER5 \
  -d currency=gbp \
  -d unit_amount=44900 \
  -d 'recurring[interval]'=month \
  -d 'metadata[tier_id]'=tier5 \
  -d 'metadata[billing_period]'=monthly
echo "✅ Tier 5 Monthly GBP: £449/month"

echo ""
echo "Creating GBP Yearly prices (with 7% discount built into price display)..."

# For yearly, we create the FULL year price (monthly × 12 × 0.93)
# Tier 1: £179 × 12 × 0.93 = £1,999.08 → £199,908 in cents
stripe prices create \
  -d product=$PRODUCT_TIER1 \
  -d currency=gbp \
  -d unit_amount=199908 \
  -d 'recurring[interval]'=year \
  -d 'metadata[tier_id]'=tier1 \
  -d 'metadata[billing_period]'=yearly
echo "✅ Tier 1 Yearly GBP: £1,999.08/year (7% discount)"

stripe prices create \
  -d product=$PRODUCT_TIER2 \
  -d currency=gbp \
  -d unit_amount=233196 \
  -d 'recurring[interval]'=year \
  -d 'metadata[tier_id]'=tier2 \
  -d 'metadata[billing_period]'=yearly
echo "✅ Tier 2 Yearly GBP: £2,331.96/year (7% discount)"

stripe prices create \
  -d product=$PRODUCT_TIER3 \
  -d currency=gbp \
  -d unit_amount=278364 \
  -d 'recurring[interval]'=year \
  -d 'metadata[tier_id]'=tier3 \
  -d 'metadata[billing_period]'=yearly
echo "✅ Tier 3 Yearly GBP: £2,783.64/year (7% discount)"

stripe prices create \
  -d product=$PRODUCT_TIER4 \
  -d currency=gbp \
  -d unit_amount=390276 \
  -d 'recurring[interval]'=year \
  -d 'metadata[tier_id]'=tier4 \
  -d 'metadata[billing_period]'=yearly
echo "✅ Tier 4 Yearly GBP: £3,902.76/year (7% discount)"

stripe prices create \
  -d product=$PRODUCT_TIER5 \
  -d currency=gbp \
  -d unit_amount=501804 \
  -d 'recurring[interval]'=year \
  -d 'metadata[tier_id]'=tier5 \
  -d 'metadata[billing_period]'=yearly
echo "✅ Tier 5 Yearly GBP: £5,018.04/year (7% discount)"

echo ""
echo "🎉 All test prices created successfully!"
echo ""
echo "You can now test the checkout flow at: http://localhost:9000/en/pricing"
echo ""
echo "Test cards:"
echo "  Success: 4242 4242 4242 4242"
echo "  Decline: 4000 0000 0000 0002"
echo ""
