import { NextRequest, NextResponse } from 'next/server'
import { lookupCustomDomain } from '@/lib/storage/blob'

/**
 * GET /api/domains/lookup
 * 
 * Internal API for middleware to look up custom domains.
 * Returns the project slug for an active custom domain.
 * 
 * Query:
 *   ?domain=docs.example.com
 * 
 * Response:
 *   { found: true, projectSlug: "my-project" }
 *   or
 *   { found: false }
 */
export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    const domain = searchParams.get('domain')
    
    if (!domain) {
      return NextResponse.json({ found: false, error: 'Missing domain parameter' })
    }
    
    const entry = await lookupCustomDomain(domain)
    
    if (entry) {
      return NextResponse.json({
        found: true,
        projectSlug: entry.projectSlug,
        status: entry.status,
      })
    }
    
    return NextResponse.json({ found: false })
    
  } catch (error) {
    console.error('[Domains Lookup] Error:', error)
    return NextResponse.json({ found: false })
  }
}
