import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'

// POST reorder sliders
export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { sliderIds } = body

    // Validation
    if (!Array.isArray(sliderIds) || sliderIds.length === 0) {
      return NextResponse.json(
        { success: false, error: 'Slider IDs array is required' },
        { status: 400 }
      )
    }

    // Update each slider's order
    const updatePromises = sliderIds.map((id: string, index: number) =>
      db.slider.update({
        where: { id },
        data: { order: index + 1 }
      })
    )

    await Promise.all(updatePromises)

    return NextResponse.json({
      success: true,
      message: 'Sliders reordered successfully'
    })
  } catch (error) {
    console.error('Error reordering sliders:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to reorder sliders' },
      { status: 500 }
    )
  }
}