import { getAutoEssays, postAutoEssay } from "@/app/www/logic/autoEssay";
import validateMillis from "@/app/www/media/validateMillis";
import validateTotem from "@/app/www/media/validateTotem";

export const GET = validateMillis(
  validateTotem(async (req, { params: { forumID } }) => {
    const avatarID = req.headers.get("avatarID") as string;

    return Response.json(await getAutoEssays(forumID, avatarID));
  }),
);

export const POST = validateMillis(
  validateTotem(async (req, { params: { forumID } }) => {
    const avatarID = req.headers.get("avatarID") as string;

    const { title, text } = await req.json();
    if (!title || !text) {
      return new Response(undefined, { status: 400 });
    }

    return Response.json(
      {
        autoEssayID: Number(
          await postAutoEssay(forumID, avatarID, title, text),
        ),
      },
      { status: 201 },
    );
  }),
);
