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

async function getTenantId() {
  const user = await getSession()
  if (!user) return { error: 'Nicht autorisiert', status: 401 }
  if (user.role !== 'TENANT_ADMIN' && user.role !== 'SERVER_ADMIN') {
    return { error: 'Keine Berechtigung', status: 403 }
  }
  if (!user.tenantId) return { error: 'Kein Mandant zugeordnet', status: 400 }
  return { tenantId: user.tenantId }
}

// GET: Returns library (all system icons) + tenant's own symbol collection
export async function GET() {
  try {
    const auth = await getTenantId()
    if ('error' in auth) return NextResponse.json({ error: auth.error }, { status: auth.status })
    const { tenantId } = auth

    // All system icons grouped by category (the library)
    const icons = await (prisma as any).iconAsset.findMany({
      where: { isActive: true },
      include: { category: { select: { id: true, name: true } } },
      orderBy: [{ category: { sortOrder: 'asc' } }, { name: 'asc' }],
    })

    const library = icons.map((icon: any) => ({
      id: icon.id,
      name: icon.name,
      mimeType: icon.mimeType,
      iconType: icon.iconType,
      categoryId: icon.categoryId,
      categoryName: icon.category?.name || 'Ohne Kategorie',
    }))

    // Tenant's own symbol collection
    const tenantSymbols = await (prisma as any).tenantSymbol.findMany({
      where: { tenantId },
      include: { icon: { select: { id: true, name: true, mimeType: true, iconType: true, category: { select: { name: true } } } } },
      orderBy: { sortOrder: 'asc' },
    })

    const mySymbols = tenantSymbols.map((ts: any) => ({
      id: ts.id,
      iconId: ts.iconId,
      name: ts.customName || ts.icon.name,
      customName: ts.customName,
      baseName: ts.icon.name,
      mimeType: ts.icon.mimeType,
      iconType: ts.icon.iconType,
      categoryName: ts.icon.category?.name || 'Ohne Kategorie',
      sortOrder: ts.sortOrder,
    }))

    return NextResponse.json({ library, mySymbols })
  } catch (error) {
    console.error('Error fetching tenant symbols:', error)
    return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
  }
}

// POST: Add a symbol from the library to "my symbols"
export async function POST(req: NextRequest) {
  try {
    const auth = await getTenantId()
    if ('error' in auth) return NextResponse.json({ error: auth.error }, { status: auth.status })
    const { tenantId } = auth

    const { iconId, customName } = await req.json()
    if (!iconId) return NextResponse.json({ error: 'iconId erforderlich' }, { status: 400 })

    // Get max sortOrder for this tenant
    const maxSort = await (prisma as any).tenantSymbol.aggregate({
      where: { tenantId },
      _max: { sortOrder: true },
    })

    const symbol = await (prisma as any).tenantSymbol.create({
      data: {
        tenantId,
        iconId,
        customName: customName || null,
        sortOrder: (maxSort._max.sortOrder ?? -1) + 1,
      },
      include: { icon: { select: { name: true, mimeType: true, iconType: true, category: { select: { name: true } } } } },
    })

    return NextResponse.json({
      id: symbol.id,
      iconId: symbol.iconId,
      name: symbol.customName || symbol.icon.name,
      customName: symbol.customName,
      baseName: symbol.icon.name,
      mimeType: symbol.icon.mimeType,
      iconType: symbol.icon.iconType,
      categoryName: symbol.icon.category?.name || 'Ohne Kategorie',
      sortOrder: symbol.sortOrder,
    })
  } catch (error) {
    console.error('Error adding tenant symbol:', error)
    return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
  }
}

// PATCH: Rename a symbol or update sortOrder
export async function PATCH(req: NextRequest) {
  try {
    const auth = await getTenantId()
    if ('error' in auth) return NextResponse.json({ error: auth.error }, { status: auth.status })
    const { tenantId } = auth

    const { id, customName, sortOrder } = await req.json()
    if (!id) return NextResponse.json({ error: 'id erforderlich' }, { status: 400 })

    const data: any = {}
    if (customName !== undefined) data.customName = customName || null
    if (sortOrder !== undefined) data.sortOrder = sortOrder

    await (prisma as any).tenantSymbol.updateMany({
      where: { id, tenantId },
      data,
    })

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Error updating tenant symbol:', error)
    return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
  }
}

// DELETE: Remove a symbol from "my symbols"
export async function DELETE(req: NextRequest) {
  try {
    const auth = await getTenantId()
    if ('error' in auth) return NextResponse.json({ error: auth.error }, { status: auth.status })
    const { tenantId } = auth

    const { id } = await req.json()
    if (!id) return NextResponse.json({ error: 'id erforderlich' }, { status: 400 })

    await (prisma as any).tenantSymbol.deleteMany({
      where: { id, tenantId },
    })

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Error deleting tenant symbol:', error)
    return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
  }
}
