export default defineEventHandler(async (event) => {
  const body = await readBody(event)
  // return body
  const data = await prisma.practitioner.create({
    data: {
      active: body.active ? true : false,
      identifier: body.identifier.map((i) => {
        return {
          name: i.name,
          value: i.value,
        }
      }),
      name: body.name.map((n) => ({
        use: n.use || 'usual',
        ...(n.family && { family: n.family }),
        ...(n.given && { given: n.given }),
        ...(n.prefix && { prefix: n.prefix }),
        ...(n.suffix && { suffix: n.suffix }),
        ...(n.period &&
          (n.period.start || n.period.end) && {
            period: Object.fromEntries(
              Object.entries({
                start: n.period.start ? new Date(n.period.start) : undefined,
                end: n.period.end ? new Date(n.period.end) : undefined,
              }).filter(([, value]) => value !== undefined),
            ),
          }),
      })),
      gender: body.gender,
      birthDate: body.birthDate ? new Date(body.birthDate) : null,
      birthPlace: body.birthPlace,
      address: body.address,
      communication: body.communication.map((c) => {
        return {
          preferred: c.preferred ? true : false,
          language: c.language,
        }
      }),
    },
  })
  return data
})
