import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getFileStream } from '@/lib/minio'
import { readFile } from 'fs/promises'
import { join } from 'path'

async function getTenantId() {
  const { headers } = await import('next/headers')
  const h = await headers()
  return h.get('x-tenant-id') || 'default'
}

export async function GET(
  _req: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const tenantId = await getTenantId()

    const symbol = await (prisma as any).tenantSymbol.findFirst({
      where: { id, tenantId },
      include: { category: true },
    })

    if (!symbol) {
      return new NextResponse('Not found', { status: 404 })
    }

    // 1. Uploaded file → MinIO
    if (symbol.isUploaded && symbol.svgPath) {
      try {
        const { stream, contentType } = await getFileStream(symbol.svgPath)
        const chunks: Buffer[] = []
        for await (const chunk of stream as any) {
          chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
        }
        const buffer = Buffer.concat(chunks)
        return new NextResponse(buffer, {
          headers: {
            'Content-Type': contentType,
            'Cache-Control': 'public, max-age=86400',
          },
        })
      } catch (err) {
        console.error('MinIO stream error:', err)
        return new NextResponse('File not available', { status: 502 })
      }
    }

    // 2. Migrated from IconAsset → serve from public/signaturen
    if (symbol.migratedFromIconId) {
      const icon = await (prisma as any).iconAsset.findUnique({
        where: { id: symbol.migratedFromIconId },
      })
      if (icon?.fileKey) {
        const filePath = join(process.cwd(), 'public', icon.fileKey)
        try {
          const buffer = await readFile(filePath)
          const ext = icon.fileKey.split('.').pop()?.toLowerCase()
          const mimeType =
            ext === 'svg' ? 'image/svg+xml' :
            ext === 'png' ? 'image/png' :
            ext === 'jpg' || ext === 'jpeg' ? 'image/jpeg' :
            'application/octet-stream'
          return new NextResponse(buffer, {
            headers: {
              'Content-Type': mimeType,
              'Cache-Control': 'public, max-age=86400',
            },
          })
        } catch {
          // fall through to 404
        }
      }
    }

    return new NextResponse('Not found', { status: 404 })
  } catch (err) {
    console.error('Tenant symbol image error:', err)
    return new NextResponse('Internal error', { status: 500 })
  }
}
