#!/bin/bash
# Create proper QAR prices with currency conversion
# Exchange rate: 1 GBP = 4.62 QAR

# Tier base prices in GBP
TIER1_GBP=179
TIER2_GBP=209
TIER3_GBP=249
TIER4_GBP=349
TIER5_GBP=449

# Convert to QAR (multiply by 4.62)
TIER1_QAR=$((179 * 462 / 100))  # 827
TIER2_QAR=$((209 * 462 / 100))  # 966
TIER3_QAR=$((249 * 462 / 100))  # 1150
TIER4_QAR=$((349 * 462 / 100))  # 1612
TIER5_QAR=$((449 * 462 / 100))  # 2074

echo "Creating QAR subscription prices with proper currency conversion..."
echo "Exchange rate: 1 GBP = 4.62 QAR"
echo ""

# Get or create products for each tier
for tier in tier1 tier2 tier3 tier4 tier5; do
  tierName="Mawidi $tier"
  
  # Check if product exists
  productId=$(stripe products list --limit 100 | jq -r ".data[] | select(.metadata.tier_id == \"$tier\" and .metadata.type != \"setup_fee\") | .id" | head -1)
  
  if [ -z "$productId" ]; then
    echo "Creating product for $tier..."
    productId=$(stripe products create \
      --name "$tierName" \
      --metadata[tier_id]="$tier" \
      | jq -r '.id')
    echo "  Created product: $productId"
  else
    echo "Using existing product for $tier: $productId"
  fi
  
  # Get the QAR amount for this tier
  case $tier in
    tier1) qar_amount=$TIER1_QAR ;;
    tier2) qar_amount=$TIER2_QAR ;;
    tier3) qar_amount=$TIER3_QAR ;;
    tier4) qar_amount=$TIER4_QAR ;;
    tier5) qar_amount=$TIER5_QAR ;;
  esac
  
  # Convert to cents
  qar_cents=$((qar_amount * 100))
  
  echo "Creating monthly QAR price for $tier: QAR $qar_amount ($qar_cents cents)"
  
  # Create monthly price
  stripe prices create \
    --product="$productId" \
    --currency=qar \
    --unit-amount=$qar_cents \
    --recurring[interval]=month \
    --metadata[tier_id]="$tier" \
    --metadata[billing_period]="monthly" \
    --metadata[currency]="QAR"
  
  echo ""
done

echo "✅ QAR prices created successfully!"
echo ""
echo "Verify prices:"
stripe prices list --limit 20 | jq '.data[] | select(.currency == "qar" and .recurring.interval == "month") | "\(.metadata.tier_id // "unknown"): QAR \(.unit_amount/100)/month"'
