package pl.firmatpp.kierowca.ui

import java.time.LocalDate
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import pl.firmatpp.kierowca.data.model.DriverLeaveCalendarDriverDto
import pl.firmatpp.kierowca.data.model.DriverLeaveCalendarEntryDto

class DriverLeaveRequestUiRulesTest {
    @Test
    fun featureIsVisibleOnlyWhenServerConfigEnablesIt() {
        assertFalse(DriverLeaveRequestUiRules.isFeatureVisible(null))
        assertFalse(DriverLeaveRequestUiRules.isFeatureVisible(LeaveRequestsConfig(enabled = false, types = listOf("URLOP"))))
        assertTrue(DriverLeaveRequestUiRules.isFeatureVisible(LeaveRequestsConfig(enabled = true, types = listOf("URLOP"))))
    }

    @Test
    fun exposesPolishStatusLabels() {
        assertEquals("Oczekuje", DriverLeaveRequestUiRules.statusLabel("pending"))
        assertEquals("Zatwierdzony", DriverLeaveRequestUiRules.statusLabel("approved"))
        assertEquals("Odrzucony", DriverLeaveRequestUiRules.statusLabel("rejected"))
        assertEquals("Anulowanie do decyzji", DriverLeaveRequestUiRules.statusLabel("cancel_requested"))
        assertEquals("Anulowany", DriverLeaveRequestUiRules.statusLabel("cancelled"))
        assertEquals("Cofnięto decyzję", DriverLeaveRequestUiRules.statusLabel("revoked"))
        assertEquals("Nieznany status", DriverLeaveRequestUiRules.statusLabel("some_raw_status"))
    }

    @Test
    fun exposesPolishEventActionLabels() {
        assertEquals("Wniosek złożony", DriverLeaveRequestUiRules.eventActionLabel("submitted"))
        assertEquals("Wniosek zatwierdzony", DriverLeaveRequestUiRules.eventActionLabel("approved"))
        assertEquals("Wniosek odrzucony", DriverLeaveRequestUiRules.eventActionLabel("rejected"))
        assertEquals("Decyzja cofnięta", DriverLeaveRequestUiRules.eventActionLabel("revoked"))
        assertEquals("Wniosek anulowany przez kierowcę", DriverLeaveRequestUiRules.eventActionLabel("cancelled_by_driver"))
        assertEquals("Kierowca poprosił o anulowanie", DriverLeaveRequestUiRules.eventActionLabel("cancellation_requested"))
        assertEquals("Anulowanie zatwierdzone", DriverLeaveRequestUiRules.eventActionLabel("cancellation_approved"))
        assertEquals("Anulowanie odrzucone", DriverLeaveRequestUiRules.eventActionLabel("cancellation_rejected"))
        assertEquals("Aktualizacja wniosku", DriverLeaveRequestUiRules.eventActionLabel("some_raw_action"))
    }

    @Test
    fun driverCanCancelPendingAndFutureApprovedRequests() {
        val tomorrow = LocalDate.now().plusDays(1).toString()
        val yesterday = LocalDate.now().minusDays(1).toString()

        assertTrue(DriverLeaveRequestUiRules.canCancel(status = "pending", dateFrom = yesterday))
        assertTrue(DriverLeaveRequestUiRules.canCancel(status = "approved", dateFrom = tomorrow))
        assertFalse(DriverLeaveRequestUiRules.canCancel(status = "approved", dateFrom = yesterday))
        assertFalse(DriverLeaveRequestUiRules.canCancel(status = "rejected", dateFrom = tomorrow))
    }

    @Test
    fun findsCalendarEntriesThatOverlapSelectedDate() {
        val entries = listOf(
            calendarEntry(id = "week", dateFrom = "2026-08-03", dateTo = "2026-08-09"),
            calendarEntry(id = "single-day", dateFrom = "2026-08-05", dateTo = "2026-08-05"),
            calendarEntry(id = "outside", dateFrom = "2026-08-10", dateTo = "2026-08-12"),
        )

        val matching = DriverLeaveRequestUiRules.calendarEntriesForDate(entries, LocalDate.parse("2026-08-05"))

        assertEquals(listOf("week", "single-day"), matching.map { it.id })
    }

    @Test
    fun normalizesPhoneNumberIntoDialerUri() {
        assertEquals("tel:+48600700800", DriverLeaveRequestUiRules.phoneDialUri("+48 600 700 800"))
        assertEquals("tel:+48600700800", DriverLeaveRequestUiRules.phoneDialUri("0048 600 700 800"))
        assertEquals(null, DriverLeaveRequestUiRules.phoneDialUri(null))
        assertEquals(null, DriverLeaveRequestUiRules.phoneDialUri(" "))
    }

    private fun calendarEntry(id: String, dateFrom: String, dateTo: String): DriverLeaveCalendarEntryDto =
        DriverLeaveCalendarEntryDto(
            id = id,
            dateFrom = dateFrom,
            dateTo = dateTo,
            type = "URLOP",
            typeLabel = "Urlop",
            status = "approved",
            driver = DriverLeaveCalendarDriverDto(
                id = "driver-$id",
                displayName = "Kierowca $id",
                phoneNumber = "+48 600 700 800",
            ),
        )
}
