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

// GET single slider
export async function GET(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const slider = await db.slider.findUnique({
      where: { id: params.id }
    })

    if (!slider) {
      return NextResponse.json(
        { success: false, error: 'Slider not found' },
        { status: 404 }
      )
    }

    return NextResponse.json({
      success: true,
      data: slider
    })
  } catch (error) {
    console.error('Error fetching slider:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to fetch slider' },
      { status: 500 }
    )
  }
}

// PUT update slider
export async function PUT(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const body = await request.json()
    const { title, subtitle, description, imageFile, imageUrl, linkUrl, displayType, isActive, order } = body

    // Check if slider exists
    const existingSlider = await db.slider.findUnique({
      where: { id: params.id }
    })

    if (!existingSlider) {
      return NextResponse.json(
        { success: false, error: 'Slider not found' },
        { status: 404 }
      )
    }

    // Validation - title is now optional, but if displayType is WITH_TEXT, title or subtitle should be provided
    const newDisplayType = displayType || existingSlider.displayType
    if (newDisplayType === 'WITH_TEXT' && (!title || title.trim() === '') && (!subtitle || subtitle.trim() === '') && (!existingSlider.title || existingSlider.title.trim() === '') && (!existingSlider.subtitle || existingSlider.subtitle.trim() === '')) {
      return NextResponse.json(
        { success: false, error: 'Title or subtitle is required when display type is "With Text"' },
        { status: 400 }
      )
    }

    const slider = await db.slider.update({
      where: { id: params.id },
      data: {
        title: title?.trim() || null,
        subtitle: subtitle?.trim() || null,
        description: description?.trim() || null,
        imageFile: imageFile?.trim() || null,
        imageUrl: imageUrl?.trim() || null,
        linkUrl: linkUrl?.trim() || null,
        displayType: displayType || existingSlider.displayType,
        isActive: isActive !== undefined ? isActive : existingSlider.isActive,
        order: order !== undefined ? order : existingSlider.order
      }
    })

    return NextResponse.json({
      success: true,
      data: slider,
      message: 'Slider updated successfully'
    })
  } catch (error) {
    console.error('Error updating slider:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to update slider' },
      { status: 500 }
    )
  }
}

// DELETE slider
export async function DELETE(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    // Check if slider exists
    const existingSlider = await db.slider.findUnique({
      where: { id: params.id }
    })

    if (!existingSlider) {
      return NextResponse.json(
        { success: false, error: 'Slider not found' },
        { status: 404 }
      )
    }

    await db.slider.delete({
      where: { id: params.id }
    })

    return NextResponse.json({
      success: true,
      message: 'Slider deleted successfully'
    })
  } catch (error) {
    console.error('Error deleting slider:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to delete slider' },
      { status: 500 }
    )
  }
}