import { Application } from 'express';
import { Resource } from 'express-automatic-routes';
import { Req, Res } from '#interfaces/IApi';
import { requireAdminApiKey } from '#middlewares/admin-api-key';
import { AdminClientService } from '#services/admin/adminService';
import { sendSuccess } from '#utils/responseUtils';

const svc = new AdminClientService();

export default (_express: Application) => {
	return <Resource>{
		post: {
			middleware: [requireAdminApiKey],
			handler: async (req: Req, res: Res) => {
				const { id } = req.params as any;
				try {
					const result = await svc.regenerateSecret(id);
					return sendSuccess(res, result);
				} catch (err: any) {
					if (err.message === 'Client not found') {
						return res.error({ message: 'Client not found', code: 'NOT_FOUND' });
					}
					return res.error(err);
				}
			},
		},
	};
};
