const adminService = require('../../services/superadmin/AdminService.js');
const { ListResponse } = require('../../utils/ListResponse.js');
const { PaginationParam } = require('../../utils/PaginationParams.js');
const { errorResponse, successResponse, messageSuccessResponse } = require('../../utils/Response.js');
const { validateCreateAdminRequest, validateUpdateAdminRequest } = require('../../validators/superadmin/admin/AdminValidators.js');

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

        const { admin, total } = await adminService.getAllAdminService({
            page, limit, search, sortBy, orderBy
        });

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

exports.showAdmin = async (req, res) => {
    try {
        const id = req.params.id;
        const data = await adminService.showAdminService(id);
        return successResponse(res, data, 'Success show admin');
    } catch (err) {
        return errorResponse(res, err);
    }
};

exports.storeAdmin = async (req, res) => {
    try {
        const validated = validateCreateAdminRequest(req.body);
        await adminService.storeAdminService(validated, req);
        return messageSuccessResponse(res, 'Admin user created successfully', 201);
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.updateAdmin = async (req, res) => {
    try {
        const id = req.params.id;
        const validatedData = validateUpdateAdminRequest(req.body);
        await adminService.updateAdminService(validatedData, id, req);
        return messageSuccessResponse(res, 'Success update admin');
    } catch (err) {
        return errorResponse(res, err);
    }
}

exports.deleteAdmin = async (req, res) => {
    try {
        const id = req.params.id;
        await adminService.deleteAdminService(id, req);
        return messageSuccessResponse(res, 'Success delete admin');
    } catch (err) {
        return errorResponse(res, err);
    }
};