package com.android.trisolarispms import androidx.compose.runtime.Composable import com.android.trisolarispms.auth.AuthzPolicy import com.android.trisolarispms.ui.AppRoute import com.android.trisolarispms.ui.auth.AuthUiState import com.android.trisolarispms.ui.auth.AuthViewModel import com.android.trisolarispms.ui.razorpay.RazorpayQrScreen import com.android.trisolarispms.ui.razorpay.RazorpaySettingsScreen import com.android.trisolarispms.ui.roomstay.ActiveRoomStaysScreen import com.android.trisolarispms.ui.roomstay.ManageRoomStayRatesScreen import com.android.trisolarispms.ui.roomstay.ManageRoomStaySelectScreen @Composable internal fun renderStayFlowRoutes( refs: MainUiRefs, state: AuthUiState, authViewModel: AuthViewModel, authz: AuthzPolicy ): Boolean { when (val currentRoute = refs.currentRoute) { is AppRoute.ActiveRoomStays -> ActiveRoomStaysScreen( propertyId = currentRoute.propertyId, propertyName = currentRoute.propertyName, onBack = { val blockBack = authz.shouldBlockBackToHome( propertyId = currentRoute.propertyId, propertyCount = state.propertyRoles.size ) if (!blockBack) { refs.route.value = AppRoute.Home } }, showBack = !authz.shouldBlockBackToHome( propertyId = currentRoute.propertyId, propertyCount = state.propertyRoles.size ), onViewRooms = { refs.route.value = AppRoute.Rooms(currentRoute.propertyId) }, onCreateBooking = { refs.route.value = AppRoute.CreateBooking(currentRoute.propertyId) }, canCreateBooking = authz.canCreateBookingFor(currentRoute.propertyId), showRazorpaySettings = authz.canManageRazorpaySettings(currentRoute.propertyId), onRazorpaySettings = { refs.route.value = AppRoute.RazorpaySettings(currentRoute.propertyId) }, showUserAdmin = authz.canManagePropertyUsers(currentRoute.propertyId), onUserAdmin = { refs.route.value = AppRoute.PropertyUsers(currentRoute.propertyId) }, onLogout = authViewModel::signOut, onManageRoomStay = { booking -> val fromAt = booking.checkInAt?.takeIf { it.isNotBlank() } ?: booking.expectedCheckInAt.orEmpty() val toAt = booking.expectedCheckOutAt?.takeIf { it.isNotBlank() } ?: booking.checkOutAt?.takeIf { it.isNotBlank() } if (fromAt.isNotBlank()) { refs.route.value = AppRoute.ManageRoomStaySelect( propertyId = currentRoute.propertyId, bookingId = booking.id.orEmpty(), fromAt = fromAt, toAt = toAt ) } }, onViewBookingStays = { booking -> refs.route.value = AppRoute.BookingRoomStays( propertyId = currentRoute.propertyId, bookingId = booking.id.orEmpty() ) }, onOpenBookingDetails = { booking -> refs.route.value = AppRoute.BookingDetailsTabs( propertyId = currentRoute.propertyId, bookingId = booking.id.orEmpty(), guestId = booking.guestId ) } ) is AppRoute.RazorpaySettings -> RazorpaySettingsScreen( propertyId = currentRoute.propertyId, onBack = { refs.openActiveRoomStays(currentRoute.propertyId) } ) is AppRoute.RazorpayQr -> RazorpayQrScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, pendingAmount = currentRoute.pendingAmount, guestPhone = currentRoute.guestPhone, onBack = { refs.route.value = AppRoute.BookingDetailsTabs( currentRoute.propertyId, currentRoute.bookingId, null ) } ) is AppRoute.ManageRoomStaySelect -> ManageRoomStaySelectScreen( propertyId = currentRoute.propertyId, bookingFromAt = currentRoute.fromAt, bookingToAt = currentRoute.toAt, onBack = { refs.openActiveRoomStays(currentRoute.propertyId) }, onNext = { rooms -> refs.selectedManageRooms.value = rooms refs.route.value = AppRoute.ManageRoomStayRates( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, fromAt = currentRoute.fromAt, toAt = currentRoute.toAt ) } ) is AppRoute.ManageRoomStaySelectFromBooking -> ManageRoomStaySelectScreen( propertyId = currentRoute.propertyId, bookingFromAt = currentRoute.fromAt, bookingToAt = currentRoute.toAt, onBack = { refs.openActiveRoomStays(currentRoute.propertyId) }, onNext = { rooms -> refs.selectedManageRooms.value = rooms refs.route.value = AppRoute.ManageRoomStayRatesFromBooking( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, guestId = currentRoute.guestId, fromAt = currentRoute.fromAt, toAt = currentRoute.toAt ) } ) is AppRoute.ManageRoomStayRates -> ManageRoomStayRatesScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, checkInAt = currentRoute.fromAt, checkOutAt = currentRoute.toAt, selectedRooms = refs.selectedManageRooms.value, onBack = { refs.route.value = AppRoute.ManageRoomStaySelect( currentRoute.propertyId, currentRoute.bookingId, currentRoute.fromAt, currentRoute.toAt ) }, onDone = { refs.openActiveRoomStays(currentRoute.propertyId) } ) is AppRoute.ManageRoomStayRatesFromBooking -> ManageRoomStayRatesScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, checkInAt = currentRoute.fromAt, checkOutAt = currentRoute.toAt, selectedRooms = refs.selectedManageRooms.value, onBack = { refs.route.value = AppRoute.ManageRoomStaySelectFromBooking( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId, currentRoute.fromAt, currentRoute.toAt ) }, onDone = { refs.route.value = AppRoute.GuestInfo( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId ) } ) else -> return false } return true }