const { getAllHospitalByAreaService, storeHospitalService, updateHospitalService, showHospitalService } = require("../../services/sales/HospitalService");
const { ListResponse } = require("../../utils/ListResponse");
const { PaginationParam } = require("../../utils/PaginationParams");
const { errorResponse, messageSuccessResponse, successResponse } = require("../../utils/Response");
const { validateStoreHospitalRequest, validateUpdateHospitalRequest } = require('../../validators/admin/hospital/HospitalValidators.js');

exports.getAllHospitalByArea = async (req, res) => {
    try {
        const { page, limit, search, sortBy, orderBy, province, city, type, ownership, progress_status } = PaginationParam(req, ['province', 'city', 'type', 'ownership', 'progress_status']);

        const { hospitals, total } = await getAllHospitalByAreaService({ page, limit, search, sortBy, orderBy, province, city, type, ownership, progress_status }, req);

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

exports.storeHospital = async (req, res) => {
    try {
        const validatedData = validateStoreHospitalRequest(req.body);
        await storeHospitalService(validatedData, req);
        return messageSuccessResponse(res, 'Success added hospital', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.updateHospital = async (req, res) => {
    try {
        const id = req.params.id;
        const validatedData = validateUpdateHospitalRequest(req.body);
        await updateHospitalService(validatedData, id, req);
        return messageSuccessResponse(res, 'Success update hospital');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.showHospital = async (req, res) => {
    try {
        const id = req.params.id;
        const data = await showHospitalService(id);
        return successResponse(res, data, 'Success show hospital');
    } catch (err) {
        return errorResponse(res, err);
    }
};