import { NextResponse } from 'next/server'
import { prisma } from '@/libs/prisma'

export async function POST(request: Request) {
  const body = await request.json()
  // @ts-ignore
  const res = await prisma.SD_SERVEY_SALES_BASIC_INFO.create({
    data: body,
  })
  return NextResponse.json(res)
}

export async function GET() {
  try {
    // @ts-ignore
    const res = await prisma.SD_SERVEY_SALES_BASIC_INFO.findMany()
    return NextResponse.json(res)
  } catch (error) {
    console.error(error)
  }
}

export async function PUT(request: Request) {
  const body = await request.json()
  console.log('🚀 ~ PUT ~ body:', body)
  const detailInfo = { ...body.detail_info, basic_info_id: body.id }
  console.log('🚀 ~ PUT ~ detailInfo:', detailInfo)
  // @ts-ignore
  const res = await prisma.SD_SERVEY_SALES_DETAIL_INFO.create({
    data: detailInfo,
  })
  return NextResponse.json({ message: 'Survey sales updated successfully' })
}
