'use client'; import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Switch } from '@/components/ui/switch'; import { Label } from '@/components/ui/label'; import { toast } from '@/hooks/use-toast'; import { Settings, Save, RefreshCw } from 'lucide-react'; interface Setting { id: string; key: string; value: string; updatedAt: Date; } interface SettingsData { booking_window_days: string; max_booking_duration_hours: string; min_booking_duration_minutes: string; booking_start_time: string; booking_end_time: string; allow_weekend_bookings: string; max_bookings_per_user_per_hour_per_day: string; allow_booking_modifications: string; booking_modification_hours_before: string; } export function AdminSettingsManagement() { const [settings, setSettings] = useState({ booking_window_days: '7', max_booking_duration_hours: '2', min_booking_duration_minutes: '30', booking_start_time: '08:00', booking_end_time: '22:00', allow_weekend_bookings: 'true', max_bookings_per_user_per_hour_per_day: '1', allow_booking_modifications: 'true', booking_modification_hours_before: '2', }); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); useEffect(() => { fetchSettings(); }, []); const fetchSettings = async () => { try { setLoading(true); const response = await fetch('/api/admin/settings'); if (response.ok) { const data = await response.json(); const settingsMap: SettingsData = { booking_window_days: '7', max_booking_duration_hours: '2', min_booking_duration_minutes: '30', booking_start_time: '08:00', booking_end_time: '22:00', allow_weekend_bookings: 'true', max_bookings_per_user_per_hour_per_day: '1', allow_booking_modifications: 'true', booking_modification_hours_before: '2', }; // Map the settings array to our object data.settings?.forEach((setting: Setting) => { if (setting.key in settingsMap) { settingsMap[setting.key as keyof SettingsData] = setting.value; } }); setSettings(settingsMap); } else { toast({ title: 'Error', description: 'Failed to fetch settings', variant: 'destructive', }); } } catch (error) { console.error('Error fetching settings:', error); toast({ title: 'Error', description: 'Failed to fetch settings', variant: 'destructive', }); } finally { setLoading(false); } }; const handleSave = async () => { try { setSaving(true); // Convert settings object to array format const settingsArray = Object.entries(settings).map(([key, value]) => ({ key, value, })); const response = await fetch('/api/admin/settings', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ settings: settingsArray }), }); if (response.ok) { toast({ title: 'Success', description: 'Settings updated successfully', }); await fetchSettings(); } else { const error = await response.json(); toast({ title: 'Error', description: error.error || 'Failed to update settings', variant: 'destructive', }); } } catch (error) { console.error('Error updating settings:', error); toast({ title: 'Error', description: 'Failed to update settings', variant: 'destructive', }); } finally { setSaving(false); } }; const updateSetting = (key: keyof SettingsData, value: string) => { setSettings((prev) => ({ ...prev, [key]: value })); }; if (loading) { return ( System Settings
{[1, 2, 3, 4].map((i) => (
))}
); } return ( System Settings
{/* Booking Window */}
updateSetting('booking_window_days', e.target.value)} />

How many days in advance users can book

{/* Max Duration */}
updateSetting('max_booking_duration_hours', e.target.value)} />

Maximum hours per booking session

{/* Min Duration */}
updateSetting('min_booking_duration_minutes', e.target.value)} />

Minimum minutes per booking session

{/* Start Time */}
updateSetting('booking_start_time', e.target.value)} />

When courts open for booking each day

{/* End Time */}
updateSetting('booking_end_time', e.target.value)} />

When courts close for booking each day

{/* Booking Restrictions */}
updateSetting('max_bookings_per_user_per_hour_per_day', e.target.value)} />

Maximum bookings per user per hour on the same day

{/* Booking Modification Settings */}
updateSetting('allow_booking_modifications', checked.toString()) } />

Whether users can edit or cancel their bookings

{/* Modification Time Restriction */}
updateSetting('booking_modification_hours_before', e.target.value)} disabled={settings.allow_booking_modifications !== 'true'} />

{settings.allow_booking_modifications === 'true' ? 'How many hours before a session users can still modify bookings' : 'Enable booking modifications to configure this setting'}

{/* Weekend Bookings */}
updateSetting('allow_weekend_bookings', checked.toString()) } />

Whether users can book courts on weekends

Current Configuration Summary

Booking Window: {settings.booking_window_days} days

Session Duration: {settings.min_booking_duration_minutes}min -{' '} {settings.max_booking_duration_hours}hrs

Operating Hours: {settings.booking_start_time} -{' '} {settings.booking_end_time}

Weekend Bookings:{' '} {settings.allow_weekend_bookings === 'true' ? 'Enabled' : 'Disabled'}

Booking Limit: {settings.max_bookings_per_user_per_hour_per_day} per hour

Booking Modifications:{' '} {settings.allow_booking_modifications === 'true' ? 'Enabled' : 'Disabled'} {settings.allow_booking_modifications === 'true' && ` (${settings.booking_modification_hours_before}h before)`}

); }