import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { safeHandler, requireAuth } from '@/lib/auth'

export const GET = safeHandler(async (_req: Request, { params }: { params: Promise<{ routeId: string }> }) => {
  await requireAuth()
  const { routeId } = await params
  const pricing = await prisma.transportPricing.findMany({
    where: { routeId },
    orderBy: { createdAt: 'desc' },
  })
  return NextResponse.json(pricing)
})

export const POST = safeHandler(async (req: Request, { params }: { params: Promise<{ routeId: string }> }) => {
  await requireAuth(['ADMIN', 'OPERATIONS'])
  const { routeId } = await params
  const { vehicleType, seasonName, startDate, endDate, pricePerTrip, pricePerKm, currency } = await req.json()

  const pricing = await prisma.transportPricing.create({
    data: {
      routeId,
      vehicleType,
      seasonName,
      startDate: new Date(startDate),
      endDate: new Date(endDate),
      pricePerTrip: parseFloat(pricePerTrip) || 0,
      pricePerKm: pricePerKm ? parseFloat(pricePerKm) : null,
      currency: currency || 'USD',
    },
  })

  return NextResponse.json(pricing, { status: 201 })
})
