import { Elysia, t } from 'elysia'
import { prisma } from './prisma'

export const portals = new Elysia({ prefix: '/portals' })
  .get('/', async () => {
    return await prisma.portal.findMany({
      orderBy: { order: 'asc' },
    })
  })
  .post(
    '/',
    async ({ body }) => {
      return await prisma.portal.create({
        data: body,
      })
    },
    {
      body: t.Object({
        title: t.String(),
        url: t.String(),
        description: t.Optional(t.String()),
        icon: t.Optional(t.String()),
        background: t.Optional(t.Boolean()),
      }),
    },
  )
  .put(
    '/:id',
    async ({ params: { id }, body }) => {
      return await prisma.portal.update({
        where: { id },
        data: body,
      })
    },
    {
      body: t.Object({
        title: t.Optional(t.String()),
        url: t.Optional(t.String()),
        description: t.Optional(t.String()),
        icon: t.Optional(t.String()),
        background: t.Optional(t.Boolean()),
      }),
    },
  )
  .delete('/:id', async ({ params: { id } }) => {
    return await prisma.portal.delete({
      where: { id },
    })
  })
