import { EMAIL_ACCESS_KEY, EMAIL_BASE_URL } from '$env/static/private';

/** @type {import('./$types').Actions} */
export const actions = {
  default: async ({ request }) => {
    const formData = await request.formData();
    console.log(formData);

    const response = await fetch(`${EMAIL_BASE_URL}/submit`, {
      method: 'POST',
      body: JSON.stringify({
        accessKey: EMAIL_ACCESS_KEY,
        subject: 'Contato - Embroidery Viewer Beta Testers!',
        name: formData.get('name'),
        email: formData.get('email'),
      }),
      headers: { 'Content-Type': 'application/json' },
    });

    const json = await response.json();
    if (json.error === undefined) {
      return {
        message: 'home.banner.feedback.success',
        textColor: 'green',
      };
    } else {
      return {
        message: 'home.banner.feedback.error',
        textColor: 'red',
      };
    }
  },
};
