import { Application } from 'express';
import { Resource } from 'express-automatic-routes';
import { FileProvider } from '#providers/FileProvider';
import { Req, Res } from '#interfaces/IApi';
import verify from '#middlewares/auth';
import { sendSuccess } from '#utils/responseUtils';

export default (_express: Application) => {
	const fileProvider = FileProvider.getInstance();
	return <Resource>{
		get: {
			middleware: [verify],
			handler: async (req: Req, res: Res) => {
				try {
					const isAdmin = req.user?.roles?.some((r) => ['admin', 'system_admin'].includes(r));
					const quota = await fileProvider.getStorageQuota(isAdmin ? undefined : req.user!.id);
					return sendSuccess(res, quota);
				} catch (error) {
					return res.error(error);
				}
			},
		},
	};
};
