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

const svc = new AdminClientService();

export default (_express: Application) => {
	return <Resource>{
		get: {
			middleware: [requireAdminApiKey],
			handler: async (req: Req, res: Res) => {
				const { id } = req.params as any;
				const client = await svc.getById(id);
				if (!client) return res.error({ message: 'Client not found', code: 'NOT_FOUND' });
				return sendSuccess(res, client);
			},
		},
		put: {
			middleware: [requireAdminApiKey, validateZod(UpdateClientSchema)],
			handler: async (req: Req, res: Res) => {
				const { id } = req.params as any;
				const client = await svc.update(id, req.body);
				return sendSuccess(res, client, 'Client updated');
			},
		},
		delete: {
			middleware: [requireAdminApiKey],
			handler: async (req: Req, res: Res) => {
				const { id } = req.params as any;
				await svc.delete(id);
				return res.status(204).send();
			},
		},
	};
};
