const vendorHistoryService = require('../../services/sales/VendorHistoryService.js');
const { ListResponse } = require('../../utils/ListResponse.js');
const { PaginationParam } = require('../../utils/PaginationParams.js');
const { errorResponse, successResponse, messageSuccessResponse } = require('../../utils/Response.js');
const { validateStoreVendorHistoryRequest } = require('../../validators/admin/vendor_history/VendorHistoriValidators.js');

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

        const { vendor_histories, total } = await vendorHistoryService.getAllVendorHistoryService({
            page, limit, search, sortBy, orderBy
        }, req);

        return ListResponse({ req, res, data: vendor_histories, page, limit, total, message: 'Vendor history successfully retrieved' });
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.showVendorHistory = async (req, res) => {
    try {
        const data = await vendorHistoryService.showVendorHistoryService(req);
        return successResponse(res, data, 'Success show vendor history');
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.storeVendorHistory = async (req, res) => {
    try {
        const validatedData = validateStoreVendorHistoryRequest(req.body);
        await vendorHistoryService.storeVendorHistoryService(validatedData, req);
        return messageSuccessResponse(res, 'Success added vendor history', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.updateVendorHistory = async (req, res) => {
    try {
        const validatedData = validateStoreVendorHistoryRequest(req.body);
        await vendorHistoryService.updateVendorHistoryService(validatedData, req);
        return messageSuccessResponse(res, 'Success update vendor history');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.deleteVendorHistory = async (req, res) => {
    try {
        await vendorHistoryService.deleteVendorHistoryService(req);
        return messageSuccessResponse(res, 'Success delete vendor history');
    } catch (err) {
        return errorResponse(res, err);
    }
};