import { defineAction } from 'astro:actions';
import { db } from '@db/database.ts';
import { Session } from '@util/session.ts';
import { Permissions } from '@util/permissions.ts';
import { z } from 'astro:schema';

export const feedback = {
  addWebsiteFeedback: defineAction({
    input: z.object({
      content: z.string()
    }),
    handler: async (input) => {
      await db.addFeedback({
        event: 'website-feedback',
        content: input.content
      });
    }
  }),
  addWebsiteContact: defineAction({
    input: z.object({
      content: z.string(),
      email: z.string().email()
    }),
    handler: async (input) => {
      await db.addFeedback({
        event: 'website-contact',
        content: `${input.content}\n\nEmail: ${input.email}`
      });
    }
  }),
  submitFeedback: defineAction({
    input: z.object({
      urlHash: z.string(),
      content: z.string()
    }),
    handler: async (input) => {
      await db.submitFeedback(input);
    }
  }),
  feedbacks: defineAction({
    input: z.object({
      includeBlanks: z.boolean()
    }),
    handler: async (input, context) => {
      Session.actionSessionFromCookies(context.cookies, Permissions.Feedback);

      return {
        feedbacks: await db.getFeedbacks({ includeBlanks: input.includeBlanks })
      };
    }
  })
};
