import {dispatchNotifications} from '@/lib/notifications';
import {env} from 'cloudflare:workers';
import {getChatGPTUser} from '@/app/chatgpt-auth';
import {authorizeModerator,moderationQueue,moderate,acceptReviewed,removeContact} from '@/lib/moderation';
import {body,response,errorResponse,ApiError} from '@/lib/commons';
export async function GET(){try{authorizeModerator((await getChatGPTUser())?.email,env.MODERATOR_EMAIL);return response({data:await moderationQueue(env.DB)})}catch(e){return errorResponse(e)}}
export async function POST(r:Request){try{authorizeModerator((await getChatGPTUser())?.email,env.MODERATOR_EMAIL);if(r.headers.get('origin')!==new URL(r.url).origin)throw new ApiError(403,'ORIGIN_REJECTED','Same-origin moderator request required');const input=await body(r);if(input.action==='dispatch_email')return response({data:await dispatchNotifications(env.DB,env)});if(input.action==='remove_contact')return response({data:await removeContact(env.DB,{task_id:input.task_id})});if(input.action==='accept'){const {action,...value}=input;const accepted=await acceptReviewed(env.DB,value);await dispatchNotifications(env.DB,env);return response({data:accepted})}return response({data:await moderate(env.DB,input)})}catch(e){return errorResponse(e)}}
