import React, { useMemo } from 'react' import { useTripStore } from '../../store/tripStore' import { useSettingsStore } from '../../store/settingsStore' import { useTranslation } from '../../i18n' import { MapPin, Clock, Calendar, Users, Sparkles } from 'lucide-react' function formatTime(timeStr, is12h) { if (!timeStr) return '' const [h, m] = timeStr.split(':').map(Number) if (is12h) { const period = h >= 12 ? 'PM' : 'AM' const h12 = h === 0 ? 12 : h > 12 ? h - 12 : h return `${h12}:${String(m).padStart(2, '0')} ${period}` } return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}` } function formatDayLabel(date, t, locale) { const now = new Date() const nowDate = now.toISOString().split('T')[0] const tomorrowUtc = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1)) const tomorrowDate = tomorrowUtc.toISOString().split('T')[0] if (date === nowDate) return t('collab.whatsNext.today') || 'Today' if (date === tomorrowDate) return t('collab.whatsNext.tomorrow') || 'Tomorrow' return new Date(date + 'T00:00:00Z').toLocaleDateString(locale || undefined, { weekday: 'short', day: 'numeric', month: 'short', timeZone: 'UTC' }) } interface TripMember { id: number username: string avatar?: string | null avatar_url?: string | null } interface WhatsNextWidgetProps { tripMembers?: TripMember[] } export default function WhatsNextWidget({ tripMembers = [] }: WhatsNextWidgetProps) { const { days, assignments } = useTripStore() const { t, locale } = useTranslation() const is12h = useSettingsStore(s => s.settings.time_format) === '12h' const upcoming = useMemo(() => { const now = new Date() const nowDate = now.toISOString().split('T')[0] const nowTime = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}` const items = [] for (const day of (days || [])) { if (!day.date) continue const dayAssignments = assignments[String(day.id)] || [] for (const a of dayAssignments) { if (!a.place) continue // Include: today (future times) + all future days const isFutureDay = day.date > nowDate const isTodayFuture = day.date === nowDate && (!a.place.place_time || a.place.place_time >= nowTime) if (isFutureDay || isTodayFuture) { items.push({ id: a.id, name: a.place.name, time: a.place.place_time, endTime: a.place.end_time, date: day.date, dayTitle: day.title, category: a.place.category, participants: (a.participants && a.participants.length > 0) ? a.participants : tripMembers.map(m => ({ user_id: m.id, username: m.username, avatar: m.avatar })), address: a.place.address, }) } } } items.sort((a, b) => { const da = a.date + (a.time || '99:99') const db = b.date + (b.time || '99:99') return da.localeCompare(db) }) return items.slice(0, 8) }, [days, assignments, tripMembers]) return (