import { Application } from 'express';
import { Resource } from 'express-automatic-routes';
import { AuditLogService } from '#services/audit/auditLogService';
import { Req, Res } from '#interfaces/IApi';
import verify from '#middlewares/auth';
import { requireAdmin } from '#middlewares/auth';
import { auditAccessLogger } from '#middlewares/audit-access-logger';
import { queryModifier } from '#middlewares/query-modifier';
import { sendSuccess } from '#utils/responseUtils';

export default (_express: Application) => {
	return <Resource>{
		get: {
			middleware: [verify, requireAdmin, queryModifier, auditAccessLogger('VIEWED')],
			handler: async (req: Req, res: Res) => {
				try {
					const { page, pageSize, sortField, sortOrder, filters } = req.payload || {};
					const result = await AuditLogService.queryAuditLogs({
						page,
						pageSize,
						sortField,
						sortOrder,
						filters,
					} as any);
					return sendSuccess(res, result);
				} catch (error) {
					return res.error(error);
				}
			},
		},
	};
};
