import { NextRequest, NextResponse } from 'next/server';
import pool from '../../../../lib/db/db';

export async function POST(request: NextRequest) {
    
    const connection = await pool.getConnection();

  try {
    const { paymentIntentId } = await request.json();

    // Verify payment in database
    const transactions: any = await connection.query(
      `SELECT * FROM transactions WHERE stripe_payment_intent_id = ? AND status = 'succeeded'`,
      [paymentIntentId]
    );

    if (transactions.length === 0) {
      return NextResponse.json({ 
        success: false, 
        error: 'Payment not found or not successful' 
      });
    }

    const transaction = transactions[0];

    return NextResponse.json({
      success: true,
      transactionId: transaction.id,
      amount: transaction.amount,
      status: transaction.status
    });

  } catch (error) {
    console.error('Error verifying payment:', error);
    return NextResponse.json(
      { success: false, error: 'Verification failed' },
      { status: 500 }
    );
  } finally {
    connection.release();
  }
}