// /pages/api/netopia/create-order.js

export default async function handler(req, res) {
	// Permitem doar POST
	if (req.method !== "POST") {
		res.setHeader("Allow", ["POST"]);
		return res.status(405).json({
			error: `Method ${req.method} not allowed`,
		});
	}

	try {
		const response = await fetch(`${process.env.NETOPIA_API_URL}/payment/card/start`, {
			method: "POST",
			headers: {
				"Content-Type": "application/json",
				Authorization: process.env.NETOPIA_API_KEY,
			},
			body: JSON.stringify(req.body),
		});

		const result = await response.json();

		const newResult = {
			payment: {
				ntpID: result.payment?.ntpID,
				status: result.payment?.status,
				paymentURL: result.payment?.paymentURL,
			},
			error: result.error || null,
		};

		return res.status(200).json(newResult);
	} catch (error) {
		console.error("Netopia init payment error:", error);

		return res.status(500).json({
			error: "Netopia request failed",
			message: error.message,
		});
	}
}
