import { CalendarView } from "@/components/calendar-view"
import { createClient } from "@/lib/supabase/server"

export default async function CalendarPage() {
    const supabase = await createClient()

    // Fetch reservations
    const { data: reservations } = await supabase
        .from('reservations')
        .select(`
      id,
      start_time,
      end_time,
      status,
      hall_id,
      halls (name),
      customers (full_name, phone)
    `)
        .neq('status', 'cancelled') // Don't show cancelled events

    // Fetch halls for filter
    const { data: halls } = await supabase
        .from('halls')
        .select('id, name')
        .order('name')

    // Transform data for calendar
    const events = reservations?.map(res => ({
        id: res.id,
        title: `${res.customers?.full_name || 'Müşteri'} ${res.customers?.phone ? `(${res.customers.phone})` : ''}`,
        start: new Date(res.start_time),
        end: new Date(res.end_time),
        resource: res,
    })) || []

    return (
        <div className="space-y-4 h-full">
            <CalendarView events={events} halls={halls || []} />
        </div>
    )
}
