import type { APIRoute } from "astro";
// import { sendEmail } from "../../../utils/email";
import {
  getOneMessage,
  newMessage,
  replyToMessage,
} from "../../../utils/message";

export const prerender = false;
// const { type}= Astro.params
export const POST: APIRoute = async ({ request, params, locals }) => {
  const data = await request.formData();
  const { type } = params;
  const name = data.get("name");
  const email = data.get("email");
  const subject = data.get("subject");
  const message = data.get("message");
  const chat_id = data.get("id");
  // console.log({ name, email, subject, message, chat_id });
  // Validate the data - you'll probably want to do more than this
  if (type?.toLowerCase() === "message") {
    if (!name || !email || !message || !subject) {
      return new Response(
        JSON.stringify({
          message: "Missing required fields",
        }),
        { status: 400 }
      );
    }
    if (
      typeof name !== "string" ||
      typeof email !== "string" ||
      typeof subject !== "string" ||
      typeof message !== "string"
    ) {
      return new Response(
        JSON.stringify({
          message: "Text Inputs Only",
        }),
        { status: 400 }
      );
    }

    // Do something with the data, then return a success response
    try {
      await newMessage({
        name,
        email,
        subject,
        message,
      });

      // await sendEmail({
      //   from: email,
      //   subject,
      //   text: message,
      //   to: "info@sambekiel.com",
      // });
    } catch (error) {
      console.error(error);
      return new Response(JSON.stringify({ error }), { status: 500 });
    }
  }
  if (type?.toLowerCase() === "reply") {
    console.log(typeof chat_id);
    if (!message || !chat_id) {
      return new Response(
        JSON.stringify({
          message: "Missing required fields",
        }),
        { status: 400 }
      );
    }
    if (
      typeof chat_id !== "string" ||
      Number(chat_id).toString().toLowerCase() === "nan" ||
      typeof message !== "string"
    ) {
      return new Response(
        JSON.stringify({
          message: "Text Inputs Only",
        }),
        { status: 400 }
      );
    }

    // Do something with the data, then return a success response
    try {
      const dbChat = await getOneMessage(Number(chat_id));

      if (!dbChat)
        return new Response(
          JSON.stringify({ error: "Internal Server Error" }),
          { status: 500 }
        );

      await replyToMessage({
        message,
        chatId: Number(chat_id),
        // @ts-ignore
        repliedBy: locals.user.id,
      });

      // await sendEmail({
      //   from: "info@sambekiel.com",
      //   subject: `RE: ${dbChat.subject}`,
      //   text: message,
      //   to: dbChat.email,
      // });
    } catch (error) {
      return new Response(JSON.stringify({ error }), { status: 500 });
    }
  }

  return new Response(
    JSON.stringify({
      message: "Success!",
    }),
    { status: 200 }
  );
};
