import { NextResponse } from 'next/server'
import { HttpStatusCode } from 'axios'
import { loggerWrapper } from '@/libs/api-wrapper'
import { axiosInstance } from '@/libs/axios'

async function setChgPwd(req: Request): Promise<NextResponse> {
  const { loginId, email, pwd, chgPwd } = await req.json()

  const res = await axiosInstance(`${process.env.NEXT_PUBLIC_QSP_API_URL}`).post(`/api/user/userPwdChg`, {
    loginId,
    chgType: 'C',
    email,
    pwd,
    chgPwd,
  })

  // console.log('🚀 ~ qsp userPwdChg api response ~ response.data:', res.data)

  if (res.data.result.resultCode !== 'S') {
    const errorMsg = res.data.result.resultMsg
    console.error(`비밀번호 변경 중 오류가 발생했습니다: ${errorMsg}`)
    return NextResponse.json({ error: `비밀번호 변경 중 오류가 발생했습니다: ${errorMsg}` }, { status: HttpStatusCode.InternalServerError })
  }

  return NextResponse.json(res.data)
}

export const POST = loggerWrapper(setChgPwd)
