#!/bin/bash
# Seed a test chargeback for testing the dashboard

set -e

echo "🌱 Seeding Test Chargeback Data"
echo "================================"
echo ""

# Get a real order with Stripe payment
ORDER_DATA=$(mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev -sN << 'SQL'
SELECT id, payment_reference
FROM orders
WHERE payment_method = 'stripe'
  AND payment_reference IS NOT NULL
ORDER BY created_at DESC
LIMIT 1;
SQL
)

ORDER_ID=$(echo "$ORDER_DATA" | awk '{print $1}')
PAYMENT_INTENT=$(echo "$ORDER_DATA" | awk '{print $2}')

if [ -z "$ORDER_ID" ]; then
    echo "❌ No Stripe orders found"
    exit 1
fi

echo "✅ Using Order ID: $ORDER_ID"
echo "✅ Payment Intent: $PAYMENT_INTENT"
echo ""

# Get payment transaction ID
TRANSACTION_ID=$(mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev -sN << SQL
SELECT id
FROM payment_transactions
WHERE transaction_id = '$PAYMENT_INTENT'
LIMIT 1;
SQL
)

echo "📋 Inserting test chargeback..."

# Generate test dispute ID
DISPUTE_ID="du_test_$(date +%s)"
CHARGE_ID="ch_test_$(date +%s)"
EVIDENCE_DUE=$(date -v+14d '+%Y-%m-%d %H:%M:%S' 2>/dev/null || date -d '+14 days' '+%Y-%m-%d %H:%M:%S')

# Insert test chargeback
mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev << SQL
INSERT INTO chargebacks (
    order_id,
    payment_transaction_id,
    gateway_dispute_id,
    gateway_charge_id,
    gateway_payment_intent_id,
    chargeback_amount,
    chargeback_reason,
    dispute_status,
    gateway,
    evidence_due_by,
    gateway_response,
    created_at,
    updated_at
) VALUES (
    $ORDER_ID,
    $([ -n "$TRANSACTION_ID" ] && echo "$TRANSACTION_ID" || echo "NULL"),
    '$DISPUTE_ID',
    '$CHARGE_ID',
    '$PAYMENT_INTENT',
    25.00,
    'fraudulent',
    'needs_response',
    'stripe',
    '$EVIDENCE_DUE',
    '{"currency":"USD","is_charge_refundable":true,"network_reason_code":"83","stripe_dispute_object":{"id":"$DISPUTE_ID","object":"dispute","amount":2500,"status":"needs_response","reason":"fraudulent"}}',
    NOW(),
    NOW()
);
SQL

echo "✅ Test chargeback created!"
echo ""

# Show the chargeback
echo "📊 Chargeback Details:"
mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev << 'SQL'
SELECT
    id as 'ID',
    order_id as 'Order',
    gateway_dispute_id as 'Dispute ID',
    dispute_status as 'Status',
    chargeback_amount as 'Amount',
    chargeback_reason as 'Reason',
    evidence_due_by as 'Evidence Due',
    created_at as 'Created'
FROM chargebacks
ORDER BY created_at DESC
LIMIT 1\G
SQL

echo ""
echo "📝 Testing API endpoint..."
echo ""

# Test the API endpoint (requires admin token)
echo "To test the API, first generate an admin token:"
echo "  php artisan admin:generate-token --email=admin@example.com --role=super_admin"
echo ""
echo "Then test the endpoint:"
echo "  curl -H 'Authorization: Bearer YOUR_TOKEN' http://localhost:8000/api/admin/financial/chargebacks"
echo ""
echo "🌐 View in admin dashboard:"
echo "   http://localhost:3001/financial/chargebacks"
echo ""
echo "✅ Test data seeded successfully!"
