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

// Resolve tenant logo to a base64 data URI (avoids exposing internal MinIO URLs)
async function resolveLogoForClient(rapport: any): Promise<string> {
  try {
    const logoUrl = rapport.data?.logoUrl
    if (logoUrl && logoUrl.startsWith('data:')) return 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] Could not resolve logo:', e)
    return ''
  }
}

// GET: Public access to rapport by token (no auth required)
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: {
        project: { select: { title: true, location: true } },
        tenant: { select: { name: true } },
        createdBy: { select: { name: true } },
      },
    })

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

    // Resolve logo to data URI so the client never sees internal MinIO URLs
    const rapportData = { ...rapport.data }
    const resolvedLogo = await resolveLogoForClient(rapport)
    if (resolvedLogo) {
      rapportData.logoUrl = resolvedLogo
    } else if (rapportData.logoUrl && !rapportData.logoUrl.startsWith('data:')) {
      rapportData.logoUrl = ''
    }

    return NextResponse.json({
      id: rapport.id,
      reportNumber: rapport.reportNumber,
      data: rapportData,
      generatedAt: rapport.generatedAt,
      project: rapport.project,
      tenant: rapport.tenant,
      createdBy: rapport.createdBy,
    })
  } catch (error) {
    console.error('Error fetching rapport by token:', error)
    return NextResponse.json({ error: 'Serverfehler' }, { status: 500 })
  }
}
