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

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