package pl.firmatpp.kierowca.ui

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import pl.firmatpp.kierowca.data.model.DriverLeaveRequestDto
import pl.firmatpp.kierowca.data.model.DriverRouteDto
import pl.firmatpp.kierowca.data.upload.RouteActionEntity
import pl.firmatpp.kierowca.data.upload.RouteActionStatus
import pl.firmatpp.kierowca.data.upload.RouteActionType
import pl.firmatpp.kierowca.ui.theme.AppThemeMode

class DriverUiStateTest {
    @Test
    fun startsOnInitializingScreenBeforeTokenCheckCompletes() {
        val state = DriverUiState()

        assertEquals(DriverScreen.Initializing, state.screen)
        assertTrue(state.loading)
    }

    @Test
    fun startsWithMaterial3ThemeMode() {
        val state = DriverUiState()

        assertEquals(AppThemeMode.Material3, state.themeMode)
    }

    @Test
    fun refreshingLeaveRequestsKeepsDetailScreenOpenAndUpdatesSelectedRequest() {
        val state = DriverUiState(
            screen = DriverScreen.LeaveRequestDetail,
            selectedLeaveRequest = leaveRequest(id = "leave-1", status = "pending"),
        )

        val refreshed = state.withLoadedLeaveRequests(
            requests = listOf(leaveRequest(id = "leave-1", status = "approved")),
            navigateToList = false,
        )

        assertEquals(DriverScreen.LeaveRequestDetail, refreshed.screen)
        assertEquals("approved", refreshed.selectedLeaveRequest?.status)
    }

    @Test
    fun openingLeaveRequestsNavigatesToList() {
        val state = DriverUiState(screen = DriverScreen.Routes)

        val refreshed = state.withLoadedLeaveRequests(
            requests = listOf(leaveRequest(id = "leave-1", status = "pending")),
            navigateToList = true,
        )

        assertEquals(DriverScreen.LeaveRequests, refreshed.screen)
        assertEquals(1, refreshed.leaveRequests.size)
    }

    @Test
    fun displayRoutesAndSelectedRouteUseLocalRouteActionsProjection() {
        val route = route(status = "ZAPLANOWANA")
        val state = DriverUiState(
            routes = listOf(route),
            selectedRoute = route,
            visibleRouteActions = listOf(action(RouteActionType.Start, RouteActionStatus.Pending, weight = 12.5)),
        )

        assertEquals("W TRAKCIE", state.displayRoutes.single().status)
        assertEquals(12.5, state.displayRoutes.single().loadingWeight)
        assertEquals("W TRAKCIE", state.displaySelectedRoute?.status)
        assertEquals(12.5, state.displaySelectedRoute?.loadingWeight)
    }

    private fun leaveRequest(id: String, status: String): DriverLeaveRequestDto =
        DriverLeaveRequestDto(
            id = id,
            dateFrom = "2026-07-10",
            dateTo = "2026-07-12",
            type = "URLOP",
            status = status,
        )

    private fun route(status: String): DriverRouteDto =
        DriverRouteDto(
            id = "1",
            startsAt = "",
            originName = "Baza",
            destinationName = "Instalacja",
            contractorName = "TPP",
            contractName = "Kontrakt",
            contractCode = "TPP-1",
            relationLabel = "Baza -> Instalacja",
            status = status,
            distanceKm = 1.0,
            notes = null,
            truck = null,
        )

    private fun action(type: String, status: String, weight: Double): RouteActionEntity =
        RouteActionEntity(
            clientActionId = "action-$type-$status",
            routeId = "1",
            action = type,
            weight = weight,
            occurredAt = "2026-06-30T07:40:00Z",
            photoClientRequestIdsJson = "[]",
            status = status,
        )
}
