const orderService = require('../services/orderService');

class OrderController {
    // 1. Yangi buyurtma yaratish
    async placeOrder(req, res) {
        try {
            const order = await orderService.createNewOrder(req.body);
            res.status(201).json(order);
        } catch (error) {
            res.status(400).json({ message: error.message });
        }
    }

    // 2. Barcha buyurtmalarni ko'rish
    async listOrders(req, res) {
        try {
            const orders = await orderService.getAllOrders();
            res.json(orders);
        } catch (error) {
            res.status(500).json({ message: error.message });
        }
    }

    // 3. Buyurtmani bekor qilish (7-variantning asosiy sharti)
    async cancel(req, res) {
        try {
            const orderId = req.params.id; // URL orqali ID ni olamiz
            const cancelledOrder = await orderService.cancelOrder(orderId);
            res.json({
                message: "Buyurtma muvaffaqiyatli bekor qilindi",
                order: cancelledOrder
            });
        } catch (error) {
            // Service'dan kelgan xatolarni (masalan, 'Completed' bo'lsa) tutib olamiz
            res.status(400).json({ message: error.message });
        }
    }
}

module.exports = new OrderController();