import { NextRequest, NextResponse } from 'next/server'
import { uploadImage, deleteImage } from '@/lib/cloudinary'
import { requireAdmin } from '@/lib/auth-helpers'

export async function POST(req: NextRequest) {
  try {
    await requireAdmin()
    const { file, folder = 'vesta-mugla' } = await req.json()
    if (!file) return NextResponse.json({ error: 'File required' }, { status: 400 })
    const result = await uploadImage(file, folder)
    return NextResponse.json({ data: result })
  } catch (error) {
    console.error('Upload error:', error)
    return NextResponse.json({ error: 'Upload failed' }, { status: 500 })
  }
}

export async function DELETE(req: NextRequest) {
  try {
    await requireAdmin()
    const { publicId } = await req.json()
    await deleteImage(publicId)
    return NextResponse.json({ success: true })
  } catch {
    return NextResponse.json({ error: 'Delete failed' }, { status: 500 })
  }
}
