// /pages/api/orders/[id].js
import { db } from "@/helpers/api";

export default async function handler(req, res) {
	if (req.method !== "GET") {
		return res.status(405).json({ error: "Method not allowed" });
	}

	try {
		const { id } = req.query;

		if (!id) {
			return res.status(400).json({ error: "Order ID is required" });
		}

		const order = await db.Orders.findByPk(id);

		if (!order) {
			return res.status(404).json({ error: "Order not found" });
		}

		// Returnăm doar datele necesare (nu expunem toate detaliile)
		const orderData = {
			id: order.id,
			status: order.status,
			total_amount: order.total_amount,
			currency: order.currency,
			pf_email: order.pf_email,
			pj_email: order.pj_email,
			createdAt: order.createdAt,
		};

		return res.status(200).json({ order: orderData });
	} catch (error) {
		console.error("Get order error:", error);
		return res.status(500).json({
			error: "Server error",
			message: error.message,
		});
	}
}
