const cityService = require('../../services/admin/CityService.js');
const { ListResponse } = require('../../utils/ListResponse.js');
const { PaginationParam } = require('../../utils/PaginationParams.js');
const { errorResponse, successResponse, messageSuccessResponse } = require('../../utils/Response.js');
const { validateStoreCityRequest } = require('../../validators/admin/city/CityValidators.js');

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

        const { cities, total } = await cityService.getAllCityService({
            page, limit, search, sortBy, orderBy
        });

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

exports.showCity = async (req, res) => {
    try {
        const id = req.params.id;
        const data = await cityService.showCityService(id);
        return successResponse(res, data, 'Success show city');
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.storeCity = async (req, res) => {
    try {
        const validatedData = validateStoreCityRequest(req.body);
        await cityService.storeCityService(validatedData, req);
        return messageSuccessResponse(res, 'Success added city', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.updateCity = async (req, res) => {
    try {
        const id = req.params.id;
        const validatedData = validateStoreCityRequest(req.body);
        await cityService.updateCityService(validatedData, id, req);
        return messageSuccessResponse(res, 'Success update city');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.deleteCity = async (req, res) => {
    try {
        const id = req.params.id;
        await cityService.deleteCityService(id, req);
        return messageSuccessResponse(res, 'Success delete city');
    } catch (err) {
        return errorResponse(res, err);
    }
};