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

// POST set landing page as default
export async function POST(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    // 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 }
      )
    }

    // Unset all other default pages
    await db.landingPage.updateMany({
      where: { 
        isDefault: true,
        id: { not: params.id }
      },
      data: { isDefault: false }
    })

    // Set this page as default
    const landingPage = await db.landingPage.update({
      where: { id: params.id },
      data: { 
        isDefault: true,
        isActive: true // Also activate it
      }
    })

    return NextResponse.json({
      success: true,
      data: landingPage,
      message: 'Landing page set as default successfully'
    })
  } catch (error) {
    console.error('Error setting default landing page:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to set default landing page' },
      { status: 500 }
    )
  }
}