"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAdminDashboard = exports.updateOrderForAdmin = exports.getOrderForAdmin = exports.listOrdersForAdmin = exports.getMyOrderById = exports.listMyOrders = exports.createOrder = void 0;
const orderService_js_1 = require("../services/orderService.js");
const apiResponse_js_1 = require("../utils/apiResponse.js");
const getCustomerId = (req) => {
    if (req.auth?.actorType !== "customer")
        return null;
    return req.auth.customer.id;
};
const getStaffId = (req) => {
    if (req.auth?.actorType !== "staff")
        return null;
    return req.auth.staff.id;
};
const createOrder = async (req, res) => {
    const customerId = getCustomerId(req);
    if (!customerId)
        return (0, apiResponse_js_1.sendError)(res, 403, "FORBIDDEN", "Customer access required.");
    try {
        const order = await (0, orderService_js_1.createCustomerOrder)(customerId, req.validatedBody);
        return (0, apiResponse_js_1.sendSuccess)(res, 201, order, "Order placed successfully.");
    }
    catch (error) {
        const message = error instanceof Error ? error.message : "Could not place order.";
        return (0, apiResponse_js_1.sendError)(res, 400, "ORDER_CREATE_FAILED", message);
    }
};
exports.createOrder = createOrder;
const listMyOrders = async (req, res) => {
    const customerId = getCustomerId(req);
    if (!customerId)
        return (0, apiResponse_js_1.sendError)(res, 403, "FORBIDDEN", "Customer access required.");
    const result = await (0, orderService_js_1.listCustomerOrders)(customerId, req.validatedQuery);
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listMyOrders = listMyOrders;
const getMyOrderById = async (req, res) => {
    const customerId = getCustomerId(req);
    if (!customerId)
        return (0, apiResponse_js_1.sendError)(res, 403, "FORBIDDEN", "Customer access required.");
    const { id } = req.validatedParams;
    const order = await (0, orderService_js_1.getCustomerOrderById)(customerId, id);
    if (!order)
        return (0, apiResponse_js_1.sendError)(res, 404, "ORDER_NOT_FOUND", "Order not found.");
    return (0, apiResponse_js_1.sendSuccess)(res, 200, order);
};
exports.getMyOrderById = getMyOrderById;
const listOrdersForAdmin = async (req, res) => {
    const result = await (0, orderService_js_1.listAdminOrders)(req.validatedQuery);
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listOrdersForAdmin = listOrdersForAdmin;
const getOrderForAdmin = async (req, res) => {
    const { id } = req.validatedParams;
    const order = await (0, orderService_js_1.getAdminOrderById)(id);
    if (!order)
        return (0, apiResponse_js_1.sendError)(res, 404, "ORDER_NOT_FOUND", "Order not found.");
    return (0, apiResponse_js_1.sendSuccess)(res, 200, order);
};
exports.getOrderForAdmin = getOrderForAdmin;
const updateOrderForAdmin = async (req, res) => {
    const staffId = getStaffId(req);
    if (!staffId)
        return (0, apiResponse_js_1.sendError)(res, 403, "FORBIDDEN", "Staff access required.");
    const { id } = req.validatedParams;
    const order = await (0, orderService_js_1.updateAdminOrder)(id, req.validatedBody, staffId);
    if (!order)
        return (0, apiResponse_js_1.sendError)(res, 404, "ORDER_NOT_FOUND", "Order not found.");
    return (0, apiResponse_js_1.sendSuccess)(res, 200, order, "Order updated.");
};
exports.updateOrderForAdmin = updateOrderForAdmin;
const getAdminDashboard = async (_req, res) => {
    const result = await (0, orderService_js_1.getDashboardSummary)();
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.getAdminDashboard = getAdminDashboard;
//# sourceMappingURL=orderController.js.map