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

// Public endpoint: get tenant info by slug (logo, name)
export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ slug: string }> }
) {
  try {
    const { slug } = await params
    const tenant = await (prisma as any).tenant.findUnique({
      where: { slug },
      select: {
        id: true,
        name: true,
        slug: true,
        logoUrl: true,
        isActive: true,
      },
    })

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

    if (!tenant.isActive) {
      return NextResponse.json({
        error: 'Mandant gesperrt',
        reason: 'suspended',
        tenant: { name: tenant.name, slug: tenant.slug, logoUrl: tenant.logoUrl },
      }, { status: 403 })
    }

    return NextResponse.json({
      tenant: {
        name: tenant.name,
        slug: tenant.slug,
        logoUrl: tenant.logoUrl,
      },
    })
  } catch (error) {
    console.error('Error fetching tenant by slug:', error)
    return NextResponse.json({ error: 'Serverfehler' }, { status: 500 })
  }
}
