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, validateQueryZod } from '#middlewares/validators';
import { CreateClientSchema, ListClientsQuerySchema } 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, validateQueryZod(ListClientsQuerySchema)],
			handler: async (req: Req, res: Res) => {
				const { page, pageSize, status, app_code, search } = req.query as any;
				const result = await svc.list({ page, pageSize, status, app_code, search: search || undefined });
				return sendSuccess(res, result);
			},
		},
		post: {
			middleware: [requireAdminApiKey, validateZod(CreateClientSchema)],
			handler: async (req: Req, res: Res) => {
				const client = await svc.create(req.body);
				res.status(201);
				return sendSuccess(res, client);
			},
		},
	};
};
