import { OrderService } from '../services/OrderService.js';
import { safeError } from '../utils/errorHandler.js';

export class OrderController {
    static async getOrders(req, res) {
        const page = parseInt(req.query.page) || 0;
        const limit = Math.min(100, parseInt(req.query.limit) || 100);
        const offset = page * limit;
        
        try {
            const orders = await OrderService.getOrders(limit, offset);
            res.json(orders);
        } catch (error) {
            safeError(res, error);
        }
    }

    static async createOrder(req, res) {
        try {
            const { productionSteps, production_steps, ...orderData } = req.body;
            const steps = productionSteps || production_steps;
            
            await OrderService.createOrder(orderData, steps);
            res.status(201).json({ success: true });
        } catch (error) {
            safeError(res, error);
        }
    }
}
