import { NextResponse } from "next/server";

// URL-urile oficiale de test și producție pentru BT iPay
const BT_IPAY_API_URL = process.env.NODE_ENV === 'production' 
  ? "https://www.btrl.ro/ipay/rest/register.do" 
  : "https://testbtrl.ro/ipay/rest/register.do";

export async function POST(req: Request) {
  try {
    const body = await req.json();
    const { courseId, billingInfo, isB2B } = body;

    const amountInBani = 200000; // MVP mock amount (2000 RON)

    // BT Pay Config
    const btConfig = {
      userName: process.env.BTPAY_USERNAME || 'test_user',
      password: process.env.BTPAY_PASSWORD || 'test_password',
      orderNumber: `RIMA-${Date.now()}`,
      amount: amountInBani,
      currency: 'RON',
      returnUrl: `${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/checkout/success?gateway=btpay`,
      description: `Inscriere curs: ${courseId}`,
    };

    console.log("[BT Pay API] Inițializare tranzacție (Nativ Next.js) cu datele:", btConfig);
    console.log("[BT Pay API] Detalii facturare:", billingInfo);

    const mockPaymentUrl = `/checkout/success?op=true&gateway=btpay&mock_order=${btConfig.orderNumber}`;

    return NextResponse.json({ url: mockPaymentUrl });
  } catch (error: any) {
    console.error("[BT Pay Error]:", error);
    return NextResponse.json(
      { error: "A apărut o eroare la procesarea plății prin BT Pay." },
      { status: 500 }
    );
  }
}
