package com.yorvana.util

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import java.time.LocalDate
import java.time.ZoneId

class DateFormatterTest {
    @Test
    fun `toDisplay should format ISO date to medium localized date`() {
        val isoDate = "2023-10-25"
        val expected = "Oct 25, 2023" // Assuming US locale for test environment, might vary
        // Since it's FormatStyle.MEDIUM, it's better to check if it's not the same and contains parts
        val result = DateFormatter.toDisplay(isoDate)
        assertThat(result).contains("2023")
        assertThat(result).contains("Oct")
    }

    @Test
    fun `toDisplay should return original string on invalid format`() {
        val invalidDate = "not-a-date"
        val result = DateFormatter.toDisplay(invalidDate)
        assertThat(result).isEqualTo(invalidDate)
    }

    @Test
    fun `fromMillis should convert milliseconds to ISO date string`() {
        val millis =
            LocalDate
                .of(2023, 10, 25)
                .atStartOfDay(ZoneId.systemDefault())
                .toInstant()
                .toEpochMilli()
        val result = DateFormatter.fromMillis(millis)
        assertThat(result).isEqualTo("2023-10-25")
    }

    @Test
    fun `toMillis should convert ISO date string to milliseconds`() {
        val isoDate = "2023-10-25"
        val expected =
            LocalDate
                .of(2023, 10, 25)
                .atStartOfDay(ZoneId.systemDefault())
                .toInstant()
                .toEpochMilli()
        val result = DateFormatter.toMillis(isoDate)
        assertThat(result).isEqualTo(expected)
    }

    @Test
    fun `toShortMonthDay should format to MMM d`() {
        val isoDate = "2023-10-25"
        val result = DateFormatter.toShortMonthDay(isoDate)
        assertThat(result).isEqualTo("Oct 25")
    }

    @Test
    fun `toYear should return the year`() {
        val isoDate = "2023-10-25"
        val result = DateFormatter.toYear(isoDate)
        assertThat(result).isEqualTo(2023)
    }

    @Test
    fun `toYear should return 0 on invalid date`() {
        val invalidDate = "invalid"
        val result = DateFormatter.toYear(invalidDate)
        assertThat(result).isEqualTo(0)
    }
}
