import { Application } from 'express';
import { Resource } from 'express-automatic-routes';
import { ResidentProvider } from '#providers/ResidentProvider';
import { Req, Res } from '#interfaces/IApi';
import verify, { requireAdmin } from '#middlewares/auth';
import { validateId } from '#middlewares/validators';
import { Errors } from '#interfaces/error';
import { sendSuccess } from '#utils/responseUtils';

export default (_express: Application) => {
	const residentProvider = ResidentProvider.getInstance();
	return <Resource>{
		delete: {
			middleware: [verify, requireAdmin, validateId],
			handler: async (req: Req, res: Res) => {
				try {
					const resident = await residentProvider.getById({ id: req.params.id! });

					if (!resident) {
						throw Errors.notFound('Resident');
					}

					const success = await residentProvider.forceDelete(req.params.id!);

					return sendSuccess(res, success);
				} catch (error) {
					return res.error(error);
				}
			},
		},
	};
};
