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

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams
    const category = searchParams.get('category')
    const keyword = searchParams.get('keyword')

    let whereCondition: any = {}
    if (category) {
      whereCondition['roof_material'] = category
    }
    if (keyword) {
      whereCondition['product_name'] = {
        contains: keyword,
      }
    }
    console.log('🚀 ~ /api/suitable/list: ~ prisma where condition:', whereCondition)

    // @ts-ignore
    const suitables = await prisma.MS_SUITABLE.findMany({
      where: whereCondition,
      orderBy: {
        product_name: 'asc',
      },
    })

    return NextResponse.json(suitables)
  } catch (error) {
    console.error('❌ 데이터 조회 중 오류가 발생했습니다:', error)
    return NextResponse.json({ error: '데이터 조회 중 오류가 발생했습니다' }, { status: 500 })
  }
}
