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

export async function GET() {
  try {
    const [launches, bestsellers, lowerPrice, promos, kits] = await Promise.all([
      db.product.findMany({
        where: { isLaunch: true },
        include: { category: true },
        orderBy: { page: 'asc' },
        take: 12,
      }),
      db.product.findMany({
        where: { isBestseller: true },
        include: { category: true },
        orderBy: { page: 'asc' },
        take: 12,
      }),
      db.product.findMany({
        where: { isLowerPriceYear: true },
        include: { category: true },
        orderBy: { page: 'asc' },
        take: 12,
      }),
      db.product.findMany({
        where: {
          promotionalPrice: { not: null },
          discountPercent: { gte: 30 },
        },
        include: { category: true },
        orderBy: { discountPercent: 'desc' },
        take: 12,
      }),
      db.kit.findMany({ take: 12 }),
    ])

    // Distinct fragrances and active ingredients for filters
    const fragrances = await db.product.findMany({
      where: { fragrance: { not: null } },
      select: { fragrance: true },
      distinct: ['fragrance'],
    })
    const actives = await db.product.findMany({
      where: { activeIngredient: { not: null } },
      select: { activeIngredient: true },
      distinct: ['activeIngredient'],
    })

    return NextResponse.json({
      status: 'success',
      data: {
        launches,
        bestsellers,
        lowerPrice,
        promos,
        kits,
        fragrances: fragrances.map(f => f.fragrance).filter(Boolean),
        actives: actives.map(a => a.activeIngredient).filter(Boolean),
      },
    })
  } catch (error) {
    console.error('Error fetching featured:', error)
    return NextResponse.json(
      { status: 'error', message: 'Erro ao buscar destaques' },
      { status: 500 }
    )
  }
}
