import { Client, LogLevel } from "@notionhq/client"

export async function collectEmail(req, res): Promise<void> {
  console.log("collecting waitlist emails")
  try {
    const { email, userId, username } = req.body
    const notion = new Client({
      auth: process.env.NOTION_API_TOKEN,
      logLevel: LogLevel.DEBUG,
    })
    // const email: string = req.query.email as string

    await notion.pages.create({
      parent: {
        database_id: process.env.NOTION_DATABASE_ID!,
      },
      properties: {
        Email: {
          title: [
            {
              text: {
                content: email,
              },
            },
          ],
        },
        Nickname: {
          rich_text: [
            {
              text: {
                content: username,
              },
            },
          ],
        },
        github_id: {
          // What is a notion text type?
          rich_text: [
            {
              text: {
                content: userId,
              },
            },
          ],
        },
      },
    })
    res.status(200).send("Email collected successfully.")
  } catch (error) {
    console.log(`Error in /cfapi/collect-email: ${error}`)
    console.log(`Error message: ${error.message}`)
    console.log(`Error stack: ${error.stack}`)
    res.status(500).send(`Error collecting emails: ${error.message}`)
  }
}
