const provinceService = require('../../services/admin/ProvinceService.js');
const { ListResponse } = require('../../utils/ListResponse.js');
const { PaginationParam } = require('../../utils/PaginationParams.js');
const { errorResponse, successResponse, messageSuccessResponse } = require('../../utils/Response.js');
const { validateStoreProvinceRequest } = require('../../validators/admin/province/ProvinceValidators.js');

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

        const { provinces, total } = await provinceService.getAllProvinceService({
            page, limit, search, sortBy, orderBy
        });

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

exports.showProvince = async (req, res) => {
    try {
        const id = req.params.id;
        const data = await provinceService.showProvinceService(id);
        return successResponse(res, data, 'Success show province');
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.storeProvince = async (req, res) => {
    try {
        const validatedData = validateStoreProvinceRequest(req.body);
        await provinceService.storeProvinceService(validatedData, req);
        return messageSuccessResponse(res, 'Success added province', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.updateProvince = async (req, res) => {
    try {
        const id = req.params.id;
        const validatedData = validateStoreProvinceRequest(req.body);
        await provinceService.updateProvinceService(validatedData, id, req);
        return messageSuccessResponse(res, 'Success update province');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.deleteProvince = async (req, res) => {
    try {
        const id = req.params.id;
        await provinceService.deleteProvinceService(id, req);
        return messageSuccessResponse(res, 'Success delete province');
    } catch (err) {
        return errorResponse(res, err);
    }
};