package com.android.trisolarispms import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.compose.BackHandler import androidx.activity.enableEdgeToEdge import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.lifecycle.viewmodel.compose.viewModel import com.android.trisolarispms.ui.AppRoute import com.android.trisolarispms.ui.auth.AuthScreen import com.android.trisolarispms.ui.auth.AuthViewModel import com.android.trisolarispms.ui.auth.NameScreen import com.android.trisolarispms.ui.auth.UnauthorizedScreen import com.android.trisolarispms.ui.booking.BookingCreateScreen import com.android.trisolarispms.ui.guest.GuestInfoScreen import com.android.trisolarispms.ui.guest.GuestSignatureScreen import com.android.trisolarispms.ui.booking.BookingExpectedDatesScreen import com.android.trisolarispms.ui.roomstay.ManageRoomStayRatesScreen import com.android.trisolarispms.ui.roomstay.ManageRoomStaySelectScreen import com.android.trisolarispms.ui.roomstay.ManageRoomStaySelection import com.android.trisolarispms.ui.roomstay.BookingRoomStaysScreen import com.android.trisolarispms.ui.roomstay.BookingDetailsTabsScreen import com.android.trisolarispms.ui.home.HomeScreen import com.android.trisolarispms.ui.payment.BookingPaymentsScreen import com.android.trisolarispms.ui.property.AddPropertyScreen import com.android.trisolarispms.ui.room.RoomFormScreen import com.android.trisolarispms.ui.room.RoomsScreen import com.android.trisolarispms.ui.card.IssueTemporaryCardScreen import com.android.trisolarispms.ui.card.CardInfoScreen import com.android.trisolarispms.ui.roomimage.RoomImagesScreen import com.android.trisolarispms.ui.roomimage.ImageTagsScreen import com.android.trisolarispms.ui.roomimage.AddImageTagScreen import com.android.trisolarispms.ui.roomimage.EditImageTagScreen import com.android.trisolarispms.ui.roomstay.ActiveRoomStaysScreen import com.android.trisolarispms.ui.payu.PayuSettingsScreen import com.android.trisolarispms.ui.payu.PayuQrScreen import com.android.trisolarispms.ui.roomtype.AddAmenityScreen import com.android.trisolarispms.ui.roomtype.AddRoomTypeScreen import com.android.trisolarispms.ui.roomtype.AmenitiesScreen import com.android.trisolarispms.ui.roomtype.EditAmenityScreen import com.android.trisolarispms.ui.roomtype.EditRoomTypeScreen import com.android.trisolarispms.ui.roomtype.RatePlanCalendarScreen import com.android.trisolarispms.ui.roomtype.RoomTypesScreen import com.android.trisolarispms.ui.theme.TrisolarisPMSTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { TrisolarisPMSTheme { val authViewModel: AuthViewModel = viewModel() val state by authViewModel.state.collectAsState() if (state.unauthorized) { UnauthorizedScreen( message = state.error ?: "Not authorized. Contact admin.", onSignOut = authViewModel::signOut ) } else if (state.apiVerified && state.needsName) { NameScreen(viewModel = authViewModel) } else if (state.apiVerified) { val route = remember { mutableStateOf(AppRoute.Home) } val refreshKey = remember { mutableStateOf(0) } val selectedPropertyId = remember { mutableStateOf(null) } val selectedPropertyName = remember { mutableStateOf(null) } val selectedRoom = remember { mutableStateOf(null) } val selectedRoomType = remember { mutableStateOf(null) } val selectedAmenity = remember { mutableStateOf(null) } val selectedGuest = remember { mutableStateOf(null) } val selectedGuestPhone = remember { mutableStateOf(null) } val selectedImageTag = remember { mutableStateOf(null) } val selectedManageRooms = remember { mutableStateOf>(emptyList()) } val roomFormKey = remember { mutableStateOf(0) } val amenitiesReturnRoute = remember { mutableStateOf(AppRoute.Home) } val currentRoute = route.value val canManageProperty: (String) -> Boolean = { propertyId -> state.isSuperAdmin || (state.propertyRoles[propertyId]?.contains("ADMIN") == true) } val canViewCardInfo: (String) -> Boolean = { propertyId -> state.isSuperAdmin || state.propertyRoles[propertyId]?.any { it == "ADMIN" || it == "MANAGER" || it == "STAFF" } == true } val canManagePayuSettings: (String) -> Boolean = { propertyId -> state.isSuperAdmin || state.propertyRoles[propertyId]?.any { it == "ADMIN" || it == "MANAGER" } == true } val canDeleteCashPayment: (String) -> Boolean = { propertyId -> state.isSuperAdmin || state.propertyRoles[propertyId]?.contains("ADMIN") == true } BackHandler(enabled = currentRoute != AppRoute.Home) { when (currentRoute) { AppRoute.Home -> Unit AppRoute.AddProperty -> route.value = AppRoute.Home is AppRoute.ActiveRoomStays -> route.value = AppRoute.Home is AppRoute.Rooms -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.AddRoom -> route.value = AppRoute.Rooms(currentRoute.propertyId) is AppRoute.EditRoom -> route.value = AppRoute.Rooms(currentRoute.propertyId) is AppRoute.RoomTypes -> route.value = AppRoute.Rooms(currentRoute.propertyId) is AppRoute.AddRoomType -> route.value = AppRoute.RoomTypes(currentRoute.propertyId) is AppRoute.EditRoomType -> route.value = AppRoute.RoomTypes(currentRoute.propertyId) AppRoute.Amenities -> route.value = amenitiesReturnRoute.value AppRoute.AddAmenity -> route.value = AppRoute.Amenities is AppRoute.EditAmenity -> route.value = AppRoute.Amenities AppRoute.ImageTags -> route.value = AppRoute.Home AppRoute.AddImageTag -> route.value = AppRoute.ImageTags is AppRoute.EditImageTag -> route.value = AppRoute.ImageTags is AppRoute.RoomImages -> route.value = AppRoute.EditRoom( currentRoute.propertyId, currentRoute.roomId ) is AppRoute.IssueTemporaryCard -> route.value = AppRoute.Rooms(currentRoute.propertyId) is AppRoute.CardInfo -> route.value = AppRoute.Rooms(currentRoute.propertyId) is AppRoute.RatePlanCalendar -> route.value = AppRoute.EditRoomType( currentRoute.propertyId, currentRoute.roomTypeId ) is AppRoute.PayuSettings -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.PayuQr -> route.value = AppRoute.BookingDetailsTabs( currentRoute.propertyId, currentRoute.bookingId, null ) is AppRoute.CreateBooking -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.GuestInfo -> route.value = AppRoute.Home is AppRoute.GuestSignature -> route.value = AppRoute.GuestInfo( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId ) is AppRoute.ManageRoomStaySelect -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.ManageRoomStayRates -> route.value = AppRoute.ManageRoomStaySelect( currentRoute.propertyId, currentRoute.bookingId, currentRoute.fromAt, currentRoute.toAt ) is AppRoute.ManageRoomStaySelectFromBooking -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.ManageRoomStayRatesFromBooking -> route.value = AppRoute.ManageRoomStaySelectFromBooking( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId, currentRoute.fromAt, currentRoute.toAt ) is AppRoute.BookingRoomStays -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.BookingExpectedDates -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.BookingDetailsTabs -> route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) is AppRoute.BookingPayments -> route.value = AppRoute.BookingDetailsTabs( currentRoute.propertyId, currentRoute.bookingId, null ) } } when (currentRoute) { AppRoute.Home -> HomeScreen( userId = state.userId, userName = state.userName, isSuperAdmin = state.isSuperAdmin, onAddProperty = { route.value = AppRoute.AddProperty }, onAmenities = { amenitiesReturnRoute.value = AppRoute.Home route.value = AppRoute.Amenities }, onImageTags = { route.value = AppRoute.ImageTags }, refreshKey = refreshKey.value, selectedPropertyId = selectedPropertyId.value, onSelectProperty = { id, name -> selectedPropertyId.value = id selectedPropertyName.value = name route.value = AppRoute.ActiveRoomStays(id, name) }, onRefreshProfile = authViewModel::refreshMe ) AppRoute.AddProperty -> AddPropertyScreen( onBack = { route.value = AppRoute.Home }, onCreated = { refreshKey.value++ route.value = AppRoute.Home } ) is AppRoute.CreateBooking -> BookingCreateScreen( propertyId = currentRoute.propertyId, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) }, onCreated = { response, guest, phone -> val bookingId = response.id.orEmpty() val guestId = (guest?.id ?: response.guestId).orEmpty() selectedGuest.value = guest selectedGuestPhone.value = phone if (bookingId.isNotBlank() && guestId.isNotBlank()) { val fromAt = response.checkInAt?.takeIf { it.isNotBlank() } ?: response.expectedCheckInAt.orEmpty() val toAt = response.expectedCheckOutAt?.takeIf { it.isNotBlank() } route.value = AppRoute.ManageRoomStaySelectFromBooking( propertyId = currentRoute.propertyId, bookingId = bookingId, guestId = guestId, fromAt = fromAt, toAt = toAt ) } else { route.value = AppRoute.Home } } ) is AppRoute.GuestInfo -> GuestInfoScreen( propertyId = currentRoute.propertyId, guestId = currentRoute.guestId, initialGuest = selectedGuest.value, initialPhone = selectedGuestPhone.value, onBack = { route.value = AppRoute.Home }, onSave = { route.value = AppRoute.GuestSignature( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId ) } ) is AppRoute.GuestSignature -> GuestSignatureScreen( propertyId = currentRoute.propertyId, guestId = currentRoute.guestId, onBack = { route.value = AppRoute.GuestInfo( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId ) }, onDone = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) } ) is AppRoute.ActiveRoomStays -> ActiveRoomStaysScreen( propertyId = currentRoute.propertyId, propertyName = currentRoute.propertyName, onBack = { route.value = AppRoute.Home }, onViewRooms = { route.value = AppRoute.Rooms(currentRoute.propertyId) }, onCreateBooking = { route.value = AppRoute.CreateBooking(currentRoute.propertyId) }, showPayuSettings = canManagePayuSettings(currentRoute.propertyId), onPayuSettings = { route.value = AppRoute.PayuSettings(currentRoute.propertyId) }, 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()) { route.value = AppRoute.ManageRoomStaySelect( propertyId = currentRoute.propertyId, bookingId = booking.id.orEmpty(), fromAt = fromAt, toAt = toAt ) } }, onViewBookingStays = { booking -> route.value = AppRoute.BookingRoomStays( propertyId = currentRoute.propertyId, bookingId = booking.id.orEmpty() ) }, onOpenBookingDetails = { booking -> route.value = AppRoute.BookingDetailsTabs( propertyId = currentRoute.propertyId, bookingId = booking.id.orEmpty(), guestId = booking.guestId ) } ) is AppRoute.PayuSettings -> PayuSettingsScreen( propertyId = currentRoute.propertyId, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) } ) is AppRoute.PayuQr -> PayuQrScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, pendingAmount = currentRoute.pendingAmount, guestPhone = currentRoute.guestPhone, onBack = { route.value = AppRoute.BookingDetailsTabs( currentRoute.propertyId, currentRoute.bookingId, null ) } ) is AppRoute.ManageRoomStaySelect -> ManageRoomStaySelectScreen( propertyId = currentRoute.propertyId, bookingFromAt = currentRoute.fromAt, bookingToAt = currentRoute.toAt, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) }, onNext = { rooms -> selectedManageRooms.value = rooms 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 = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) }, onNext = { rooms -> selectedManageRooms.value = rooms 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 = selectedManageRooms.value, onBack = { route.value = AppRoute.ManageRoomStaySelect( currentRoute.propertyId, currentRoute.bookingId, currentRoute.fromAt, currentRoute.toAt ) }, onDone = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) } ) is AppRoute.ManageRoomStayRatesFromBooking -> ManageRoomStayRatesScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, checkInAt = currentRoute.fromAt, checkOutAt = currentRoute.toAt, selectedRooms = selectedManageRooms.value, onBack = { route.value = AppRoute.ManageRoomStaySelectFromBooking( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId, currentRoute.fromAt, currentRoute.toAt ) }, onDone = { route.value = AppRoute.GuestInfo( currentRoute.propertyId, currentRoute.bookingId, currentRoute.guestId ) } ) is AppRoute.BookingRoomStays -> BookingRoomStaysScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) } ) is AppRoute.BookingExpectedDates -> BookingExpectedDatesScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, status = currentRoute.status, expectedCheckInAt = currentRoute.expectedCheckInAt, expectedCheckOutAt = currentRoute.expectedCheckOutAt, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) }, onDone = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) } ) is AppRoute.BookingDetailsTabs -> BookingDetailsTabsScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, guestId = currentRoute.guestId, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) }, onEditCheckout = { expectedCheckInAt, expectedCheckOutAt -> route.value = AppRoute.BookingExpectedDates( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, status = "CHECKED_IN", expectedCheckInAt = expectedCheckInAt, expectedCheckOutAt = expectedCheckOutAt ) }, onEditSignature = { guestId -> route.value = AppRoute.GuestSignature( currentRoute.propertyId, currentRoute.bookingId, guestId ) }, onOpenPayuQr = { pendingAmount, guestPhone -> route.value = AppRoute.PayuQr( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, pendingAmount = pendingAmount, guestPhone = guestPhone ) }, onOpenPayments = { route.value = AppRoute.BookingPayments( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId ) }, canManageDocuments = canManagePayuSettings(currentRoute.propertyId) ) is AppRoute.BookingPayments -> BookingPaymentsScreen( propertyId = currentRoute.propertyId, bookingId = currentRoute.bookingId, canAddCash = canManagePayuSettings(currentRoute.propertyId), canDeleteCash = canDeleteCashPayment(currentRoute.propertyId), onBack = { route.value = AppRoute.BookingDetailsTabs( currentRoute.propertyId, currentRoute.bookingId, null ) } ) is AppRoute.Rooms -> RoomsScreen( propertyId = currentRoute.propertyId, onBack = { route.value = AppRoute.ActiveRoomStays( currentRoute.propertyId, selectedPropertyName.value ?: "Property" ) }, onAddRoom = { roomFormKey.value++ route.value = AppRoute.AddRoom(currentRoute.propertyId) }, onViewRoomTypes = { route.value = AppRoute.RoomTypes(currentRoute.propertyId) }, onViewCardInfo = { route.value = AppRoute.CardInfo(currentRoute.propertyId) }, canManageRooms = canManageProperty(currentRoute.propertyId), canViewCardInfo = canViewCardInfo(currentRoute.propertyId), onEditRoom = { selectedRoom.value = it roomFormKey.value++ route.value = AppRoute.EditRoom(currentRoute.propertyId, it.id ?: "") }, onIssueTemporaryCard = { if (it.id != null) { selectedRoom.value = it route.value = AppRoute.IssueTemporaryCard(currentRoute.propertyId, it.id) } } ) is AppRoute.RoomTypes -> RoomTypesScreen( propertyId = currentRoute.propertyId, onBack = { route.value = AppRoute.Rooms(currentRoute.propertyId) }, onAdd = { route.value = AppRoute.AddRoomType(currentRoute.propertyId) }, canManageRoomTypes = canManageProperty(currentRoute.propertyId), onEdit = { selectedRoomType.value = it route.value = AppRoute.EditRoomType(currentRoute.propertyId, it.id ?: "") } ) is AppRoute.AddRoomType -> AddRoomTypeScreen( propertyId = currentRoute.propertyId, onBack = { route.value = AppRoute.RoomTypes(currentRoute.propertyId) }, onSave = { route.value = AppRoute.RoomTypes(currentRoute.propertyId) } ) is AppRoute.EditRoomType -> EditRoomTypeScreen( propertyId = currentRoute.propertyId, roomType = selectedRoomType.value ?: com.android.trisolarispms.data.api.model.RoomTypeDto(id = currentRoute.roomTypeId, code = "", name = ""), onBack = { route.value = AppRoute.RoomTypes(currentRoute.propertyId) }, onSave = { route.value = AppRoute.RoomTypes(currentRoute.propertyId) }, onOpenRatePlanCalendar = { ratePlanId, ratePlanCode -> route.value = AppRoute.RatePlanCalendar( currentRoute.propertyId, currentRoute.roomTypeId, ratePlanId, ratePlanCode ) } ) AppRoute.Amenities -> AmenitiesScreen( onBack = { route.value = amenitiesReturnRoute.value }, onAdd = { route.value = AppRoute.AddAmenity }, canManageAmenities = state.isSuperAdmin, onEdit = { selectedAmenity.value = it route.value = AppRoute.EditAmenity(it.id ?: "") } ) AppRoute.AddAmenity -> AddAmenityScreen( onBack = { route.value = AppRoute.Amenities }, onSave = { route.value = AppRoute.Amenities } ) is AppRoute.EditAmenity -> EditAmenityScreen( amenity = selectedAmenity.value ?: com.android.trisolarispms.data.api.model.AmenityDto(id = currentRoute.amenityId, name = ""), onBack = { route.value = AppRoute.Amenities }, onSave = { route.value = AppRoute.Amenities } ) AppRoute.ImageTags -> ImageTagsScreen( onBack = { route.value = AppRoute.Home }, onAdd = { route.value = AppRoute.AddImageTag }, onEdit = { selectedImageTag.value = it route.value = AppRoute.EditImageTag(it.id ?: "") } ) AppRoute.AddImageTag -> AddImageTagScreen( onBack = { route.value = AppRoute.ImageTags }, onSave = { route.value = AppRoute.ImageTags } ) is AppRoute.EditImageTag -> EditImageTagScreen( tag = selectedImageTag.value ?: com.android.trisolarispms.data.api.model.RoomImageTagDto(id = currentRoute.tagId, name = ""), onBack = { route.value = AppRoute.ImageTags }, onSave = { route.value = AppRoute.ImageTags } ) is AppRoute.AddRoom -> RoomFormScreen( title = "Add Room", propertyId = currentRoute.propertyId, roomId = null, roomData = null, formKey = roomFormKey.value, onBack = { route.value = AppRoute.Rooms(currentRoute.propertyId) }, onSave = { route.value = AppRoute.Rooms(currentRoute.propertyId) }, onViewImages = { } ) is AppRoute.EditRoom -> RoomFormScreen( title = "Modify Room", propertyId = currentRoute.propertyId, roomId = currentRoute.roomId, roomData = selectedRoom.value, formKey = roomFormKey.value, onBack = { route.value = AppRoute.Rooms(currentRoute.propertyId) }, onSave = { route.value = AppRoute.Rooms(currentRoute.propertyId) }, onViewImages = { roomId -> route.value = AppRoute.RoomImages(currentRoute.propertyId, roomId) } ) is AppRoute.IssueTemporaryCard -> IssueTemporaryCardScreen( propertyId = currentRoute.propertyId, roomId = currentRoute.roomId, roomNumber = selectedRoom.value?.roomNumber?.toString(), onBack = { route.value = AppRoute.Rooms(currentRoute.propertyId) } ) is AppRoute.CardInfo -> CardInfoScreen( propertyId = currentRoute.propertyId, onBack = { route.value = AppRoute.Rooms(currentRoute.propertyId) } ) is AppRoute.RatePlanCalendar -> RatePlanCalendarScreen( propertyId = currentRoute.propertyId, ratePlanId = currentRoute.ratePlanId, ratePlanCode = currentRoute.ratePlanCode, onBack = { route.value = AppRoute.EditRoomType(currentRoute.propertyId, currentRoute.roomTypeId) } ) is AppRoute.RoomImages -> RoomImagesScreen( propertyId = currentRoute.propertyId, roomId = currentRoute.roomId, onBack = { route.value = AppRoute.EditRoom(currentRoute.propertyId, currentRoute.roomId) } ) } } else { AuthScreen(viewModel = authViewModel) } } } } }