package dev.privacyllc.period.core.database

import androidx.room.TypeConverter
import java.time.Instant
import java.time.LocalDate

/**
 * Dates are stored as epoch days and instants as epoch milliseconds — integers,
 * not strings.
 *
 * A `LocalDate` written as text sorts correctly by luck of ISO-8601 and compares
 * incorrectly the moment anybody writes a locale-aware formatter into the path.
 * An epoch day has one representation, sorts and ranges in SQL, and cannot carry
 * a timezone by accident — which matters here more than usual, because a cycle
 * is measured in whole days and PRODUCT_PLAN.md §50 calls out timezone and DST
 * edge cases as things that must be tested.
 */
internal class Converters {
    @TypeConverter fun dateToEpochDay(value: LocalDate?): Long? = value?.toEpochDay()
    @TypeConverter fun epochDayToDate(value: Long?): LocalDate? = value?.let(LocalDate::ofEpochDay)

    @TypeConverter fun instantToEpochMilli(value: Instant?): Long? = value?.toEpochMilli()
    @TypeConverter fun epochMilliToInstant(value: Long?): Instant? = value?.let(Instant::ofEpochMilli)
}
