import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import { prisma } from '@/lib/prisma'
import { revalidatePath } from 'next/cache'

export async function POST(req: NextRequest) {
  try {
    // Check authentication
    const session = await auth()
    if (!session?.user?.id) {
      return NextResponse.json(
        { success: false, error: 'Unauthorized' },
        { status: 401 }
      )
    }

    // Delete all notes for the user (cascade will handle labels-note relationships)
    const result = await prisma.note.deleteMany({
      where: {
        userId: session.user.id
      }
    })

    // Delete all labels for the user
    await prisma.label.deleteMany({
      where: {
        userId: session.user.id
      }
    })

    // Delete all notebooks for the user
    await prisma.notebook.deleteMany({
      where: {
        userId: session.user.id
      }
    })

    // Revalidate paths
    revalidatePath('/')
    revalidatePath('/settings/data')

    return NextResponse.json({
      success: true,
      deletedNotes: result.count
    })
  } catch (error) {
    console.error('Delete all error:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to delete notes' },
      { status: 500 }
    )
  }
}
