"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteAdminBrand = exports.updateAdminBrand = exports.createAdminBrand = exports.getAdminBrandById = exports.listAdminBrands = void 0;
const brandService_js_1 = require("../services/brandService.js");
const apiResponse_js_1 = require("../utils/apiResponse.js");
const getStaffId = (req) => {
    if (req.auth?.actorType === "staff") {
        return req.auth.staff.id;
    }
    throw new Error("UNAUTHORIZED");
};
const listAdminBrands = async (req, res) => {
    const query = req.validatedQuery;
    const result = await (0, brandService_js_1.listBrands)(query);
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listAdminBrands = listAdminBrands;
const getAdminBrandById = async (req, res) => {
    const { id } = req.validatedParams;
    const brand = await (0, brandService_js_1.getBrandById)(id);
    if (!brand) {
        return (0, apiResponse_js_1.sendError)(res, 404, "BRAND_NOT_FOUND", "Brand not found.");
    }
    return (0, apiResponse_js_1.sendSuccess)(res, 200, brand);
};
exports.getAdminBrandById = getAdminBrandById;
const createAdminBrand = async (req, res) => {
    const input = req.validatedBody;
    const staffId = getStaffId(req);
    try {
        const brand = await (0, brandService_js_1.createBrand)(input, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 201, brand, "Brand created.");
    }
    catch (error) {
        if (error.message === "BRAND_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "BRAND_NOT_FOUND", "Brand not found.");
        }
        throw error;
    }
};
exports.createAdminBrand = createAdminBrand;
const updateAdminBrand = async (req, res) => {
    const { id } = req.validatedParams;
    const input = req.validatedBody;
    const staffId = getStaffId(req);
    try {
        const brand = await (0, brandService_js_1.updateBrand)(id, input, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 200, brand, "Brand updated.");
    }
    catch (error) {
        if (error.message === "BRAND_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "BRAND_NOT_FOUND", "Brand not found.");
        }
        throw error;
    }
};
exports.updateAdminBrand = updateAdminBrand;
const deleteAdminBrand = async (req, res) => {
    const { id } = req.validatedParams;
    try {
        await (0, brandService_js_1.deleteBrand)(id);
        return (0, apiResponse_js_1.sendSuccess)(res, 200, null, "Brand deleted.");
    }
    catch (error) {
        if (error.message === "BRAND_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "BRAND_NOT_FOUND", "Brand not found.");
        }
        throw error;
    }
};
exports.deleteAdminBrand = deleteAdminBrand;
//# sourceMappingURL=brandController.js.map