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

// Convert a MinIO/internal logo URL to a base64 data URI server-side
async function resolveLogoDataUri(rapport: any): Promise<string> {
  try {
    const logoUrl = rapport.data?.logoUrl
    // Already a data URI — use as-is
    if (logoUrl && logoUrl.startsWith('data:')) return logoUrl

    // Try to load logo from MinIO via tenant's logoFileKey / logoUrl
    const tenantId = rapport.tenantId
    if (!tenantId) return ''

    const tenant = await (prisma as any).tenant.findUnique({
      where: { id: tenantId },
      select: { logoFileKey: true, logoUrl: true },
    })

    let fileKey = tenant?.logoFileKey
    if (!fileKey && tenant?.logoUrl) {
      const match = tenant.logoUrl.match(/logos\/[^?]+/)
      if (match) fileKey = match[0]
    }
    if (!fileKey) return ''

    const { getFileStream } = await import('@/lib/minio')
    const { stream, contentType } = await getFileStream(fileKey)
    const chunks: Buffer[] = []
    for await (const chunk of stream as AsyncIterable<Buffer>) {
      chunks.push(chunk)
    }
    const buffer = Buffer.concat(chunks)
    return `data:${contentType};base64,${buffer.toString('base64')}`
  } catch (e) {
    console.warn('[Rapport PDF] Could not resolve logo:', e)
    return ''
  }
}

// GET: Generate and serve PDF for a rapport (public, token-based)
export async function GET(req: NextRequest, { params }: { params: Promise<{ token: string }> }) {
  try {
    const { token } = await params
    const rapport = await (prisma as any).rapport.findUnique({
      where: { token },
      include: {
        tenant: { select: { name: true } },
      },
    })

    if (!rapport) {
      return NextResponse.json({ error: 'Rapport nicht gefunden' }, { status: 404 })
    }

    // Ensure logo is a valid data URI for PDF rendering
    const pdfData = { ...rapport.data }
    const resolvedLogo = await resolveLogoDataUri(rapport)
    if (resolvedLogo) {
      pdfData.logoUrl = resolvedLogo
    } else if (pdfData.logoUrl && !pdfData.logoUrl.startsWith('data:')) {
      // Remove non-data-URI logo URLs — @react-pdf can't fetch them
      pdfData.logoUrl = ''
    }

    // Dynamic import to avoid issues during build when @react-pdf/renderer isn't installed
    const { renderToBuffer } = await import('@react-pdf/renderer')
    const { RapportDocument } = await import('@/lib/rapport-pdf')

    const buffer = await renderToBuffer(
      React.createElement(RapportDocument, { data: pdfData }) as any
    )

    return new NextResponse(Buffer.from(buffer) as any, {
      headers: {
        'Content-Type': 'application/pdf',
        'Content-Disposition': `inline; filename="Rapport-${rapport.reportNumber}.pdf"`,
        'Cache-Control': 'public, max-age=3600',
      },
    })
  } catch (error: any) {
    console.error('Error generating rapport PDF:', error)
    const msg = error?.message || String(error)
    return NextResponse.json({ error: 'PDF-Generierung fehlgeschlagen', detail: msg }, { status: 500 })
  }
}
