#!/bin/bash
# Test chargeback with a real payment intent from the database

set -e

echo "🧪 Testing Chargeback with Real Payment"
echo "========================================"
echo ""

# Get the most recent Stripe payment intent
PAYMENT_INTENT=$(mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev -sN << 'SQL'
SELECT payment_reference
FROM orders
WHERE payment_method = 'stripe'
  AND payment_reference IS NOT NULL
ORDER BY created_at DESC
LIMIT 1;
SQL
)

if [ -z "$PAYMENT_INTENT" ]; then
    echo "❌ No Stripe payments found in database"
    echo "   Please create a test booking with Stripe payment first"
    exit 1
fi

echo "✅ Found payment intent: $PAYMENT_INTENT"
echo ""

# Get order details
echo "📋 Order Details:"
mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev << SQL
SELECT
    id as 'Order ID',
    session_id as 'Session',
    payment_reference as 'Payment Intent',
    total_amount as 'Amount',
    status as 'Status',
    created_at as 'Created'
FROM orders
WHERE payment_reference = '$PAYMENT_INTENT';
SQL

echo ""
echo "🔧 Creating test dispute for this payment..."
echo ""

# Create a dispute using Stripe CLI
# Note: This will only work if the payment intent exists in your Stripe account
stripe disputes create \
  --payment-intent="$PAYMENT_INTENT" \
  --reason=fraudulent 2>&1 || {
    echo ""
    echo "⚠️  Could not create dispute via API (expected - test payments may not support this)"
    echo ""
    echo "📝 Manual Testing Options:"
    echo ""
    echo "Option 1: Use Stripe Dashboard"
    echo "  1. Go to: https://dashboard.stripe.com/test/payments"
    echo "  2. Find payment: $PAYMENT_INTENT"
    echo "  3. Click 'Create test dispute'"
    echo "  4. Select reason: 'Fraudulent'"
    echo ""
    echo "Option 2: Create a new test order with dispute-triggering card"
    echo "  1. Visit: http://localhost:3002/booking/1"
    echo "  2. Select seats and proceed to checkout"
    echo "  3. Use test card: 4000000000000259 (Always triggers disputes)"
    echo "  4. Complete booking"
    echo "  5. Dispute will be created automatically"
    echo ""
    echo "Option 3: Use stripe trigger with webhook inspection"
    echo "  stripe trigger charge.dispute.created"
    echo "  (Then manually insert the test data into your database)"
    exit 0
}

echo ""
echo "✅ Dispute created!"
echo ""
echo "🔍 Checking results..."
sleep 2

# Check for chargeback in database
echo ""
echo "📊 Chargebacks in database:"
mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev << 'SQL'
SELECT
    id,
    order_id,
    gateway_dispute_id,
    dispute_status,
    chargeback_amount,
    chargeback_reason,
    created_at
FROM chargebacks
ORDER BY created_at DESC
LIMIT 3;
SQL

echo ""
echo "📝 Recent webhook events:"
mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev << 'SQL'
SELECT
    event_type,
    event_id,
    processed_at,
    created_at
FROM stripe_webhook_events
WHERE event_type LIKE '%dispute%'
ORDER BY created_at DESC
LIMIT 5;
SQL

echo ""
echo "✅ Test complete!"
echo ""
echo "🌐 View in admin dashboard:"
echo "   http://localhost:3001/financial/chargebacks"
