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<{ id: string }> }) => {
  await requireAuth()
  const { id } = await params
  const slots = await prisma.activitySlot.findMany({
    where: { activityId: id },
    orderBy: { startTime: 'asc' },
  })
  return NextResponse.json(slots)
})

export const POST = safeHandler(async (req: Request, { params }: { params: Promise<{ id: string }> }) => {
  await requireAuth(['ADMIN', 'OPERATIONS'])
  const { id } = await params
  const {
    startTime, endTime, daysOfWeek, seasonName, startDate, endDate,
    adultPrice, childPrice, groupPrice, groupMinSize, currency, maxBookings,
  } = await req.json()

  const slot = await prisma.activitySlot.create({
    data: {
      activityId: id,
      startTime,
      endTime: endTime || null,
      daysOfWeek: daysOfWeek || null,
      seasonName,
      startDate: new Date(startDate),
      endDate: new Date(endDate),
      adultPrice: parseFloat(adultPrice) || 0,
      childPrice: parseFloat(childPrice) || 0,
      groupPrice: groupPrice ? parseFloat(groupPrice) : null,
      groupMinSize: groupMinSize ? parseInt(groupMinSize) : null,
      currency: currency || 'USD',
      maxBookings: maxBookings ? parseInt(maxBookings) : null,
    },
  })

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