const { ListResponse } = require("../../../utils/ListResponse");
const { formatISOWithoutTimezone, formatDateOnly } = require("../../../utils/FormatDate.js");

const formatItem = (item) => ({
    ...item,
    start_term: formatDateOnly(item.start_term),
    end_term: formatDateOnly(item.end_term),
    createdAt: formatISOWithoutTimezone(item.createdAt),
    updatedAt: formatISOWithoutTimezone(item.updatedAt),
});

exports.ExecutivesHistoriCollection = (req, res, data = [], total = null, page = 1, limit = 10, message = 'Success') => {
    const formattedData = data.map(formatItem);

    if (typeof total !== 'number') {
        return res.status(200).json({
            success: true,
            message,
            data: Array.isArray(formattedData)
        });
    }

    return ListResponse({ req, res, data: formattedData, total, page, limit, message });
};