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 { sendSuccess } from '#utils/responseUtils';

export default (_express: Application) => {
	return <Resource>{
		get: {
			middleware: [verify, requireAdmin, auditAccessLogger('VIEWED')],
			handler: async (req: Req, res: Res) => {
				try {
					const { entityType, entityId } = req.params;
					const history = await AuditLogService.getEntityChangeHistory(entityType as string, entityId as string);
					return sendSuccess(res, history);
				} catch (error) {
					return res.error(error);
				}
			},
		},
	};
};
