package com.prodhack.moscow2025.presentation.utils

import org.junit.Test
import org.junit.Assert.*
import java.time.Instant
import java.time.ZoneId
import java.util.*

class TimeUtilsTest {

	@Test
	fun `daysUntilTimestampZoned calculates correct days for future timestamp`() {
		val now = Instant.now()
		val futureTimestamp = now.plusSeconds(5 * 24 * 60 * 60).toEpochMilli() // 5 days in future
		val days = daysUntilTimestampZoned(futureTimestamp)
		assertTrue(days >= 4 && days <= 5) // Allow some margin for execution time
	}

	@Test
	fun `daysUntilTimestampZoned calculates correct days for past timestamp`() {
		val now = Instant.now()
		val pastTimestamp = now.minusSeconds(3 * 24 * 60 * 60).toEpochMilli() // 3 days in past
		val days = daysUntilTimestampZoned(pastTimestamp)
		assertTrue(days <= -2 && days >= -4) // Should be negative
	}

	@Test
	fun `getStartOfDayTimestamp returns start of day`() {
		val date = Date(1234567890000L) // Some specific date
		val startOfDay = getStartOfDayTimestamp(date)
		val dateFromTimestamp = Date(startOfDay)
		val calendar = Calendar.getInstance()
		calendar.time = dateFromTimestamp
		assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY))
		assertEquals(0, calendar.get(Calendar.MINUTE))
		assertEquals(0, calendar.get(Calendar.SECOND))
	}

	@Test
	fun `getStartOfTodayTimestamp returns today start`() {
		val startOfToday = getStartOfTodayTimestamp()
		val now = System.currentTimeMillis()
		val today = getStartOfDayTimestamp(Date(now))
		// Should be same day
		assertEquals(today, startOfToday)
	}

	@Test
	fun `timestampToDate formats correctly`() {
		val timestamp = 1234567890000L
		val formatted = timestampToDate(timestamp)
		// Format should be dd.MM
		assertTrue(formatted.matches(Regex("\\d{2}\\.\\d{2}")))
	}

	@Test
	fun `timestampToDateWithYear formats correctly`() {
		val timestamp = 1234567890000L
		val formatted = timestampToDateWithYear(timestamp)
		// Format should be dd.MM.YYYY
		assertTrue(formatted.matches(Regex("\\d{2}\\.\\d{2}\\.\\d{4}")))
	}

	@Test
	fun `convertGMTToSystemTimezone converts correctly`() {
		val gmtTimestamp = 1234567890000L
		val converted = convertGMTToSystemTimezone(gmtTimestamp)
		// Should return start of day timestamp
		val expected = getStartOfDayTimestamp(Date(gmtTimestamp))
		assertEquals(expected, converted)
	}

	@Test
	fun `timestampToIso converts to ISO string`() {
		val timestamp = 1234567890000L
		val iso = timestampToIso(timestamp)
		// Should be valid ISO format
		assertTrue(iso.contains("T") || iso.contains("Z"))
		assertTrue(iso.isNotEmpty())
	}
}

