const { PaginationParam } = require("../../utils/PaginationParams");
const statusHistoryService = require('../../services/sales/StatusHistoryService.js');
const { validateCreateStatusHisotryRequest } = require("../../validators/admin/status_history/StatusHistoryValidators.js");
const { StatusHistoryCollection } = require("../../resources/admin/status_history/StatusHistoryCollection.js");
const { errorResponse, messageSuccessResponse } = require("../../utils/Response.js");

exports.getAllStatusHistory = async (req, res) => {
    try {
        const { page, limit, search, sortBy, orderBy } = PaginationParam(req);

        const { status_histories, total } = await statusHistoryService.getAllStatusHistoryService({
            page, limit, search, sortBy, orderBy
        }, req);

        return StatusHistoryCollection(req, res, status_histories, total, page, limit, 'Status history successfully retrieved');
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.storeStatusHistory = async (req, res) => {
    try {
        const validatedData = validateCreateStatusHisotryRequest(req.body);
        await statusHistoryService.storeStatusHistoryService(validatedData, req);
        return messageSuccessResponse(res, 'Success added status history', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}