"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteAdminClientReview = exports.updateAdminClientReview = exports.createAdminClientReview = exports.getAdminClientReviewById = exports.listAdminClientReviews = void 0;
const clientReviewService_js_1 = require("../services/clientReviewService.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 listAdminClientReviews = async (req, res) => {
    const query = req.validatedQuery;
    const result = await (0, clientReviewService_js_1.listClientReviews)(query);
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listAdminClientReviews = listAdminClientReviews;
const getAdminClientReviewById = async (req, res) => {
    const { id } = req.validatedParams;
    const review = await (0, clientReviewService_js_1.getClientReviewById)(id);
    if (!review) {
        return (0, apiResponse_js_1.sendError)(res, 404, "CLIENT_REVIEW_NOT_FOUND", "Client review not found.");
    }
    return (0, apiResponse_js_1.sendSuccess)(res, 200, review);
};
exports.getAdminClientReviewById = getAdminClientReviewById;
const createAdminClientReview = async (req, res) => {
    const input = req.validatedBody;
    const staffId = getStaffId(req);
    try {
        const review = await (0, clientReviewService_js_1.createClientReview)(input, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 201, review, "Client review created.");
    }
    catch (error) {
        if (error.message === "CLIENT_REVIEW_CREATE_FAILED") {
            return (0, apiResponse_js_1.sendError)(res, 500, "CLIENT_REVIEW_CREATE_FAILED", "Failed to create client review.");
        }
        throw error;
    }
};
exports.createAdminClientReview = createAdminClientReview;
const updateAdminClientReview = async (req, res) => {
    const { id } = req.validatedParams;
    const input = req.validatedBody;
    const staffId = getStaffId(req);
    try {
        const review = await (0, clientReviewService_js_1.updateClientReview)(id, input, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 200, review, "Client review updated.");
    }
    catch (error) {
        if (error.message === "CLIENT_REVIEW_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "CLIENT_REVIEW_NOT_FOUND", "Client review not found.");
        }
        throw error;
    }
};
exports.updateAdminClientReview = updateAdminClientReview;
const deleteAdminClientReview = async (req, res) => {
    const { id } = req.validatedParams;
    try {
        await (0, clientReviewService_js_1.deleteClientReview)(id);
        return (0, apiResponse_js_1.sendSuccess)(res, 200, null, "Client review deleted.");
    }
    catch (error) {
        if (error.message === "CLIENT_REVIEW_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "CLIENT_REVIEW_NOT_FOUND", "Client review not found.");
        }
        throw error;
    }
};
exports.deleteAdminClientReview = deleteAdminClientReview;
//# sourceMappingURL=clientReviewController.js.map