import { ActivityIndicator, Alert, Pressable, ScrollView, StyleSheet, Text, View, } from 'react-native'; import { useReservations } from '@/features/reservations/hooks/useReservations'; function getReservationWindowStart(date: string, startTime: string): Date { return new Date(`${date}T${startTime}:00`); } function getReservationWindowEnd(date: string, endTime: string): Date { return new Date(`${date}T${endTime}:00`); } function canCheckIn(date: string, startTime: string, endTime: string): boolean { const now = new Date(); const reservationStart = getReservationWindowStart(date, startTime); const reservationEnd = getReservationWindowEnd(date, endTime); const checkInOpensAt = new Date(reservationStart.getTime() - 15 * 60 * 1000); return now >= checkInOpensAt && now <= reservationEnd; } function hasToDate(value: unknown): value is { toDate: () => Date } { return ( !!value && typeof value === 'object' && 'toDate' in value && typeof value.toDate === 'function' ); } function formatCheckInAt(value: unknown): string { if (!hasToDate(value)) { return 'Registrada'; } const date = value.toDate(); if (!(date instanceof Date) || Number.isNaN(date.getTime())) { return 'Registrada'; } return new Intl.DateTimeFormat('es-ES', { dateStyle: 'short', timeStyle: 'short', }).format(date); } export default function ReservationsScreen() { const { activeReservations, historyReservations, loading, error, reload, cancelReservation, cancelingReservationId, checkingInReservationId, checkInReservation, } = useReservations(); const handleCancelReservation = (reservationId: string) => { Alert.alert( 'Cancelar reserva', 'Esta acción marcará la reserva como cancelada.', [ { text: 'Volver', style: 'cancel' }, { text: 'Cancelar reserva', style: 'destructive', onPress: () => { void cancelReservation(reservationId); }, }, ] ); }; if (loading) { return ( ); } if (error) { return ( {error} Reintentar ); } return ( Mis reservas Activas {activeReservations.length === 0 ? ( No tienes reservas activas ) : ( activeReservations.map((item) => ( {item.officeName} Activa Fecha: {item.date} Inicio: {item.startTime} Fin: {item.endTime} Llegada: {item.checkInAt ? formatCheckInAt(item.checkInAt) : 'Pendiente'} {!item.checkInAt && canCheckIn(item.date, item.startTime, item.endTime) ? ( { void checkInReservation(item.id); }} disabled={checkingInReservationId === item.id} > {checkingInReservationId === item.id ? 'Registrando llegada...' : 'He llegado'} ) : null} handleCancelReservation(item.id)} disabled={cancelingReservationId === item.id} > {cancelingReservationId === item.id ? 'Cancelando...' : 'Cancelar reserva'} )) )} Historial {historyReservations.length === 0 ? ( No tienes reservas en el historial ) : ( historyReservations.map((item) => ( {item.officeName} {item.status === 'completed' ? 'Completada' : 'Cancelada'} Fecha: {item.date} Inicio: {item.startTime} Fin: {item.endTime} Llegada: {item.checkInAt ? formatCheckInAt(item.checkInAt) : 'No registrada'} )) )} ); } const styles = StyleSheet.create({ container: { flex: 1, padding: 16, }, center: { flex: 1, alignItems: 'center', justifyContent: 'center', padding: 24, }, title: { fontSize: 28, fontWeight: '700', marginBottom: 16, }, list: { gap: 12, paddingBottom: 24, }, section: { gap: 12, }, sectionTitle: { fontSize: 20, fontWeight: '700', }, card: { borderWidth: 1, borderColor: '#ddd', borderRadius: 12, padding: 16, gap: 6, }, cardHeader: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: 12, }, cardTitle: { fontSize: 18, fontWeight: '600', flex: 1, }, statusBadge: { borderRadius: 999, paddingHorizontal: 10, paddingVertical: 4, }, statusActive: { backgroundColor: '#d7f5df', }, statusCancelled: { backgroundColor: '#f3d8d8', }, statusCompleted: { backgroundColor: '#dce8f8', }, statusText: { fontSize: 12, fontWeight: '700', color: '#333', }, error: { color: 'red', marginBottom: 12, }, button: { backgroundColor: '#1f6feb', paddingHorizontal: 16, paddingVertical: 12, borderRadius: 10, }, buttonText: { color: '#fff', fontWeight: '600', }, cancelButton: { backgroundColor: '#c62828', paddingVertical: 12, borderRadius: 10, alignItems: 'center', marginTop: 8, }, checkInButton: { backgroundColor: '#2e7d32', paddingVertical: 12, borderRadius: 10, alignItems: 'center', marginTop: 8, }, empty: { color: '#666', }, });