import { externalApi } from '../_api.ts';
import { z } from 'astro:schema';
import { db } from '@db/database.ts';
import { BASE_PATH } from 'astro:env/server';

export const POST = externalApi({
  input: z.object({
    event: z.string(),
    title: z.string(),
    users: z.array(z.string())
  }),
  auth: true,
  handler: async ({ input }) => {
    const feedbacks = await db.addUserFeedbacks({
      event: input.event,
      title: input.title,
      uuids: input.users
    });

    const response = feedbacks.map((feedback) => ({
      uuid: feedback.uuid,
      url: `${BASE_PATH}/feedback/${feedback.urlHash}`
    }));

    return new Response(JSON.stringify({ feedback: response }), { status: 200 });
  }
});
