const orderRepository = require('../repositories/orderRepository');
const OrderStatus = require('../domain/value-objects/OrderStatus');

class OrderService {
    async createNewOrder(orderData) {
        orderData.status = OrderStatus.PENDING;
        return await orderRepository.create(orderData);
    }
    async getAllOrders() {
        return await orderRepository.findAll();
    }
    async cancelOrder(orderId) {
        const order = await orderRepository.findById(orderId);
        
        if (!order) {
            throw new Error("Buyurtma topilmadi!");
        }
          
        if (order.status === OrderStatus.COMPLETED) {
            throw new Error("Yakunlangan buyurtmani bekor qilib bo'lmaydi!");
        }

        if (order.status === OrderStatus.CANCELLED) {
            throw new Error("Bu buyurtma allaqachon bekor qilingan!");
        }

        return await orderRepository.updateStatus(orderId, OrderStatus.CANCELLED);
    }
}

module.exports = new OrderService();