import { Elysia } from 'elysia'
import { cors } from '@elysiajs/cors'
import { auth } from './auth'
import { prisma } from './prisma'
import { resources } from './resources'
import { posts } from './posts'
import { portals } from './portals'

const app = new Elysia()
  .use(
    cors({
      origin: 'http://localhost:5173',
      methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
      credentials: true,
      allowedHeaders: ['Content-Type', 'Authorization'],
    }),
  )
  .mount(auth.handler)
  .get('/health', () => 'OK')
  .use(resources)
  .use(portals)
  .use(posts)
  .get('/user/:id', async ({ params }) => {
    const user = await prisma.user.findUnique({
      where: {
        id: params.id,
      },
    })
    return user
  })

app.listen(3001, () => {
  console.log('Server running on port 3001')
})

export default app
export type App = typeof app
