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

// POST toggle landing page active status
export async function POST(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const body = await request.json()
    const { isActive } = body

    if (typeof isActive !== 'boolean') {
      return NextResponse.json(
        { success: false, error: 'isActive must be a boolean' },
        { status: 400 }
      )
    }

    // Check if landing page exists
    const existingPage = await db.landingPage.findUnique({
      where: { id: params.id }
    })

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

    const landingPage = await db.landingPage.update({
      where: { id: params.id },
      data: { isActive }
    })

    return NextResponse.json({
      success: true,
      data: landingPage,
      message: `Landing page ${isActive ? 'activated' : 'deactivated'} successfully`
    })
  } catch (error) {
    console.error('Error toggling landing page status:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to toggle landing page status' },
      { status: 500 }
    )
  }
}