import {NextResponse} from 'next/server'

import DAL from '@/lib/server/dal'

import {LoginRequestSchema} from '@/lib/schemas/LoginRequestSchema'
import {verifyPassword} from '@/lib/server/utils'
import {validateSchema} from '@/lib/server/utils/actionUtils'
import {wrapper} from '@/lib/server/utils/apiWrapper'
import {createJwtToken} from '@/lib/server/utils/jwtUtils'

export const POST = wrapper(async (request): Promise<NextResponse> => {
  const {errors, data} = validateSchema(LoginRequestSchema, await request.json())
  console.log(data, errors)

  if (errors) return new NextResponse(JSON.stringify(errors), {status: 400})

  const user = await DAL.getUserByEmail(data.username)
  if (!user) throw new Error('Unauthorized')

  if (!user.password) throw new Error('Unauthorized')


  const isValidPassword = verifyPassword(user.password, data.password)

  if (!isValidPassword) throw new Error('Unauthorized')

  const token = createJwtToken(user)
  return new NextResponse(JSON.stringify({token}), {
    status: 200,
  })
}, false)
