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

exports.getAllHospital = 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 hospitalService.getAllHospitalService({
            page, limit, search, sortBy, orderBy, province, city, type, ownership, progress_status
        });

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

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

exports.storeHospital = async (req, res) => {
    try {
        const validatedData = validateStoreHospitalRequest(req.body);
        await hospitalService.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 hospitalService.updateHospitalService(validatedData, id, req);
        return messageSuccessResponse(res, 'Success update hospital');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.deleteHospital = async (req, res) => {
    try {
        const id = req.params.id;
        await hospitalService.deleteHospitalService(id, req);
        return messageSuccessResponse(res, 'Success delete hospital');
    } catch (err) {
        return errorResponse(res, err);
    }
};