import Link from 'next/link';
import { ArrowLeft, ChevronLeft, ChevronRight } from 'lucide-react';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { Badge } from '@kit/ui/badge';
import { Button } from '@kit/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card';
import { createBookingManagementApi } from '@kit/booking-management/api';
import { CmsPageShell } from '~/components/cms-page-shell';
interface PageProps {
params: Promise<{ account: string }>;
}
const WEEKDAYS = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'];
const MONTH_NAMES = [
'Januar',
'Februar',
'März',
'April',
'Mai',
'Juni',
'Juli',
'August',
'September',
'Oktober',
'November',
'Dezember',
];
function getDaysInMonth(year: number, month: number): number {
return new Date(year, month + 1, 0).getDate();
}
function getFirstWeekday(year: number, month: number): number {
const day = new Date(year, month, 1).getDay();
// Convert Sunday=0 to Monday-based (Mo=0, Di=1 … So=6)
return day === 0 ? 6 : day - 1;
}
function isDateInRange(date: string, checkIn: string, checkOut: string): boolean {
return date >= checkIn && date < checkOut;
}
export default async function BookingCalendarPage({ params }: PageProps) {
const { account } = await params;
const client = getSupabaseServerClient();
const { data: acct } = await client
.from('accounts')
.select('id')
.eq('slug', account)
.single();
if (!acct) return
Konto nicht gefunden
;
const api = createBookingManagementApi(client);
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const daysInMonth = getDaysInMonth(year, month);
const firstWeekday = getFirstWeekday(year, month);
// Load bookings for this month
const monthStart = `${year}-${String(month + 1).padStart(2, '0')}-01`;
const monthEnd = `${year}-${String(month + 1).padStart(2, '0')}-${String(daysInMonth).padStart(2, '0')}`;
const bookings = await api.listBookings(acct.id, {
from: monthStart,
to: monthEnd,
page: 1,
});
// Build set of occupied dates
const occupiedDates = new Set();
for (const booking of bookings.data) {
const b = booking as Record;
if (b.status === 'cancelled' || b.status === 'no_show') continue;
const checkIn = String(b.check_in ?? '');
const checkOut = String(b.check_out ?? '');
if (!checkIn || !checkOut) continue;
for (let d = 1; d <= daysInMonth; d++) {
const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
if (isDateInRange(dateStr, checkIn, checkOut)) {
occupiedDates.add(dateStr);
}
}
}
// Build calendar grid cells
const cells: Array<{ day: number | null; occupied: boolean; isToday: boolean }> = [];
// Empty cells before first day
for (let i = 0; i < firstWeekday; i++) {
cells.push({ day: null, occupied: false, isToday: false });
}
const todayStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
for (let d = 1; d <= daysInMonth; d++) {
const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
cells.push({
day: d,
occupied: occupiedDates.has(dateStr),
isToday: dateStr === todayStr,
});
}
// Fill remaining cells to complete the grid
while (cells.length % 7 !== 0) {
cells.push({ day: null, occupied: false, isToday: false });
}
return (
{/* Header */}
Belegungskalender
Zimmerauslastung im Überblick
{/* Month Navigation */}
{MONTH_NAMES[month]} {year}
{/* Weekday Header */}
{WEEKDAYS.map((day) => (
{day}
))}
{/* Calendar Grid */}
{cells.map((cell, idx) => (
{cell.day !== null && (
<>
{cell.day}
{cell.occupied && (
)}
>
)}
))}
{/* Legend */}
{/* Summary */}
Buchungen in diesem Monat
{bookings.data.length}
{occupiedDates.size} von {daysInMonth} Tagen belegt
);
}