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

// GET /api/labels/[id] - Get a specific label
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await auth()
  if (!session?.user?.id) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  try {
    const { id } = await params
    const label = await prisma.label.findUnique({
      where: { id },
      include: {
        notebook: {
          select: { id: true, name: true }
        }
      }
    })

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

    // Verify ownership
    if (label.notebookId) {
      const notebook = await prisma.notebook.findUnique({
        where: { id: label.notebookId },
        select: { userId: true }
      })
      if (notebook?.userId !== session.user.id) {
        return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
      }
    } else if (label.userId !== session.user.id) {
      return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
    }

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

// PUT /api/labels/[id] - Update a label
export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await auth()
  if (!session?.user?.id) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  try {
    const { id } = await params
    const body = await request.json()
    const { name, color } = body

    // Get the current label first
    const currentLabel = await prisma.label.findUnique({
      where: { id },
      include: {
        notebook: {
          select: { id: true, userId: true }
        }
      }
    })

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

    // Verify ownership
    if (currentLabel.notebookId) {
      if (currentLabel.notebook?.userId !== session.user.id) {
        return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
      }
    } else if (currentLabel.userId !== session.user.id) {
      return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
    }

    const newName = name ? name.trim() : currentLabel.name

    // For backward compatibility, update old label field in notes if renaming
    const targetUserIdPut = currentLabel.userId || currentLabel.notebook?.userId || session.user.id;
    if (name && name.trim() !== currentLabel.name && targetUserIdPut) {
      const allNotes = await prisma.note.findMany({
        where: {
          userId: targetUserIdPut,
          labels: { not: null }
        },
        select: { id: true, labels: true }
      })

      for (const note of allNotes) {
        if (note.labels) {
          try {
            const noteLabels: string[] = Array.isArray(note.labels) ? (note.labels as string[]) : []
            const updatedLabels = noteLabels.map(l =>
              l.toLowerCase() === currentLabel.name.toLowerCase() ? newName : l
            )

            if (JSON.stringify(updatedLabels) !== JSON.stringify(noteLabels)) {
              await prisma.note.update({
                where: { id: note.id },
                data: {
                  labels: updatedLabels as any
                }
              })
            }
          } catch (e) {
          }
        }
      }
    }

    // Now update the label record
    const label = await prisma.label.update({
      where: { id },
      data: {
        ...(name && { name: newName }),
        ...(color && { color })
      }
    })

    // Revalidate to refresh UI
    revalidatePath('/')

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

// DELETE /api/labels/[id] - Delete a label
export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await auth()
  if (!session?.user?.id) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  try {
    const { id } = await params

    // First, get the label to know its name and userId
    const label = await prisma.label.findUnique({
      where: { id },
      include: {
        notebook: {
          select: { id: true, userId: true }
        }
      }
    })

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

    // Verify ownership
    if (label.notebookId) {
      if (label.notebook?.userId !== session.user.id) {
        return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
      }
    } else if (label.userId !== session.user.id) {
      return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
    }

    // For backward compatibility, remove from old label field in notes
    const targetUserIdDel = label.userId || label.notebook?.userId || session.user.id;
    if (targetUserIdDel) {
      const allNotes = await prisma.note.findMany({
        where: {
          userId: targetUserIdDel,
          labels: { not: null }
        },
        select: { id: true, labels: true }
      })

      for (const note of allNotes) {
        if (note.labels) {
          try {
            const noteLabels: string[] = Array.isArray(note.labels) ? (note.labels as string[]) : []
            const filteredLabels = noteLabels.filter(
              l => l.toLowerCase() !== label.name.toLowerCase()
            )

            if (filteredLabels.length !== noteLabels.length) {
              await prisma.note.update({
                where: { id: note.id },
                data: {
                  labels: (filteredLabels.length > 0 ? filteredLabels : null) as any
                }
              })
            }
          } catch (e) {
          }
        }
      }
    }

    // Now delete the label record
    await prisma.label.delete({
      where: { id }
    })

    // Revalidate to refresh UI
    revalidatePath('/')

    return NextResponse.json({
      success: true,
      message: `Label "${label.name}" deleted successfully`
    })
  } catch (error) {
    console.error('Error deleting label:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to delete label' },
      { status: 500 }
    )
  }
}
