const salesService = require('../../services/admin/SalesService.js');
const { ListResponse } = require('../../utils/ListResponse.js');
const { PaginationParam } = require('../../utils/PaginationParams.js');
const { errorResponse, successResponse, messageSuccessResponse } = require('../../utils/Response.js');
const { validateCreateSalesRequest, validateUpdateSalesRequest } = require('../../validators/admin/sales/SalesValidators.js');

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

        const { sales, total } = await salesService.getAllSalesService({
            page, limit, search, sortBy, orderBy
        });

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

exports.showSales = async (req, res) => {
    try {
        const id = req.params.id;
        const data = await salesService.showSalesService(id);
        return successResponse(res, data, 'Success show sales');
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.storeSales = async (req, res) => {
    try {
        const validated = validateCreateSalesRequest(req.body);
        await salesService.storeSalesService(validated, req);
        return messageSuccessResponse(res, 'Sales user created successfully', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.updateSales = async (req, res) => {
    try {
        const id = req.params.id;
        const validatedData = validateUpdateSalesRequest(req.body);
        await salesService.updateSalesService(validatedData, id, req);
        return messageSuccessResponse(res, 'Success update sales');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.deleteSales = async (req, res) => {
    try {
        const id = req.params.id;
        await salesService.deleteSalesService(id, req);
        return messageSuccessResponse(res, 'Success delete sales');
    } catch (err) {
        return errorResponse(res, err);
    }
};