import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { checkAdminAuth } from '@/lib/auth-check'

// POST /api/admin/category-images - Create or update category image (Admin only)
export async function POST(request: Request) {
  const authError = await checkAdminAuth()
  if (authError) return authError

  try {
    const body = await request.json()

    const categoryImage = await prisma.categoryImage.upsert({
      where: { categoryName: body.categoryName },
      update: { imageUrl: body.imageUrl },
      create: {
        categoryName: body.categoryName,
        imageUrl: body.imageUrl,
      },
    })

    return NextResponse.json(categoryImage, { status: 201 })
  } catch (error) {
    console.error('Error saving category image:', error)
    return NextResponse.json({ error: 'Failed to save category image' }, { status: 500 })
  }
}
