// src/app/(frontend)/api/search/suggestions/route.ts
// Auto-complete suggestions endpoint

import { NextRequest, NextResponse } from 'next/server'
import { getPayload } from 'payload'
import config from '@payload-config'
import { getSearchSuggestions } from '@/lib/search'
import {
  searchLimiter,
  rateLimitHeaders,
  getClientIpFromRequest,
  isIpBlocked,
} from '@/lib/security'

// Validation constants
const MIN_QUERY_LENGTH = 2
const MAX_QUERY_LENGTH = 50
const MAX_LIMIT = 10
const DEFAULT_LIMIT = 5
const SEARCH_RATE_LIMIT = 30

export async function GET(request: NextRequest) {
  try {
    // IP-Blocklist prüfen
    const ip = getClientIpFromRequest(request)
    if (isIpBlocked(ip)) {
      return NextResponse.json(
        { suggestions: [] },
        { status: 403 },
      )
    }

    // Rate limiting (zentral)
    const rateLimit = await searchLimiter.check(ip)

    if (!rateLimit.allowed) {
      return NextResponse.json(
        { suggestions: [] },
        {
          status: 429,
          headers: rateLimitHeaders(rateLimit, SEARCH_RATE_LIMIT),
        },
      )
    }

    // Parse query parameters
    const { searchParams } = new URL(request.url)
    const query = searchParams.get('q')?.trim() || ''
    const category = searchParams.get('category')?.trim()
    const tenantParam = searchParams.get('tenant')
    const limitParam = searchParams.get('limit')
    const localeParam = searchParams.get('locale')?.trim()

    // Validate locale
    const validLocales = ['de', 'en']
    const locale = localeParam && validLocales.includes(localeParam) ? localeParam : 'de'

    // Validate query
    if (!query || query.length < MIN_QUERY_LENGTH) {
      return NextResponse.json(
        { suggestions: [] },
        {
          headers: {
            ...rateLimitHeaders(rateLimit, SEARCH_RATE_LIMIT),
            'Cache-Control': 'public, max-age=60, s-maxage=60',
          },
        },
      )
    }

    if (query.length > MAX_QUERY_LENGTH) {
      return NextResponse.json(
        { error: `Query must be at most ${MAX_QUERY_LENGTH} characters` },
        { status: 400 }
      )
    }

    // Parse numeric parameters
    const limit = Math.min(
      Math.max(1, parseInt(limitParam || String(DEFAULT_LIMIT), 10) || DEFAULT_LIMIT),
      MAX_LIMIT
    )
    const tenantId = tenantParam ? parseInt(tenantParam, 10) : undefined

    // Validate tenant ID if provided
    if (tenantParam && (isNaN(tenantId!) || tenantId! < 1)) {
      return NextResponse.json(
        { error: 'Invalid tenant ID' },
        { status: 400 }
      )
    }

    // Get payload instance
    const payload = await getPayload({ config })

    // Get suggestions
    const suggestions = await getSearchSuggestions(payload, {
      query,
      tenantId,
      categorySlug: category,
      locale,
      limit,
    })

    return NextResponse.json(
      { suggestions },
      {
        headers: {
          ...rateLimitHeaders(rateLimit, SEARCH_RATE_LIMIT),
          'Cache-Control': 'public, max-age=60, s-maxage=60',
        },
      },
    )
  } catch (error) {
    console.error('[Suggestions API] Error:', error)
    return NextResponse.json(
      { suggestions: [] },
      { status: 500 }
    )
  }
}
