"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.archiveAdminProduct = exports.updateAdminProduct = exports.createAdminProduct = exports.getAdminProductById = exports.listAdminProducts = exports.getPublicProductBySlug = exports.getPublicProductFilters = exports.listPublicProducts = void 0;
const productFilters_js_1 = require("../services/productFilters.js");
const productService_js_1 = require("../services/productService.js");
const apiResponse_js_1 = require("../utils/apiResponse.js");
const getStaffId = (req) => {
    if (req.auth?.actorType !== "staff")
        return null;
    return req.auth.staff.id;
};
const listPublicProducts = async (req, res) => {
    const query = req.validatedQuery;
    const result = await (0, productService_js_1.listProducts)(query, { includeAllStatuses: false });
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listPublicProducts = listPublicProducts;
const getPublicProductFilters = async (_req, res) => {
    const result = await (0, productFilters_js_1.getProductFilters)();
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.getPublicProductFilters = getPublicProductFilters;
const getPublicProductBySlug = async (req, res) => {
    const { slug } = req.validatedParams;
    const product = await (0, productService_js_1.getProductBySlug)(slug, false);
    if (!product) {
        return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_NOT_FOUND", "Product not found.");
    }
    return (0, apiResponse_js_1.sendSuccess)(res, 200, product);
};
exports.getPublicProductBySlug = getPublicProductBySlug;
const listAdminProducts = async (req, res) => {
    const query = req.validatedQuery;
    const result = await (0, productService_js_1.listProducts)(query, { includeAllStatuses: true });
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listAdminProducts = listAdminProducts;
const getAdminProductById = async (req, res) => {
    const { id } = req.validatedParams;
    const product = await (0, productService_js_1.getProductById)(id);
    if (!product) {
        return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_NOT_FOUND", "Product not found.");
    }
    return (0, apiResponse_js_1.sendSuccess)(res, 200, product);
};
exports.getAdminProductById = getAdminProductById;
const createAdminProduct = async (req, res) => {
    const staffId = getStaffId(req);
    if (!staffId)
        return (0, apiResponse_js_1.sendError)(res, 403, "FORBIDDEN", "Staff access required.");
    try {
        const product = await (0, productService_js_1.createProduct)(req.validatedBody, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 201, product, "Product created.");
    }
    catch (error) {
        const message = error instanceof Error ? error.message : "Could not create product.";
        if (message.includes("Duplicate") || message.includes("unique")) {
            return (0, apiResponse_js_1.sendError)(res, 409, "PRODUCT_CONFLICT", "Product SKU or slug already exists.");
        }
        return (0, apiResponse_js_1.sendError)(res, 400, "PRODUCT_CREATE_FAILED", message);
    }
};
exports.createAdminProduct = createAdminProduct;
const updateAdminProduct = 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;
    try {
        const product = await (0, productService_js_1.updateProduct)(id, req.validatedBody, staffId);
        if (!product)
            return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_NOT_FOUND", "Product not found.");
        return (0, apiResponse_js_1.sendSuccess)(res, 200, product, "Product updated.");
    }
    catch (error) {
        const message = error instanceof Error ? error.message : "Could not update product.";
        if (message.includes("Duplicate") || message.includes("unique")) {
            return (0, apiResponse_js_1.sendError)(res, 409, "PRODUCT_CONFLICT", "Product SKU or slug already exists.");
        }
        return (0, apiResponse_js_1.sendError)(res, 400, "PRODUCT_UPDATE_FAILED", message);
    }
};
exports.updateAdminProduct = updateAdminProduct;
const archiveAdminProduct = async (req, res) => {
    const { id } = req.validatedParams;
    const product = await (0, productService_js_1.archiveProduct)(id);
    if (!product) {
        return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_NOT_FOUND", "Product not found.");
    }
    return (0, apiResponse_js_1.sendSuccess)(res, 200, product, "Product archived.");
};
exports.archiveAdminProduct = archiveAdminProduct;
//# sourceMappingURL=productController.js.map