package com.syaroful.agrilinkvocpro.control_feature.core.utils

import java.time.Duration
import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

fun getRelativeTime(isoTime: String): String {
    return try {
        val formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME
        val dateTime = OffsetDateTime.parse(isoTime, formatter)
        val now = OffsetDateTime.now(ZoneId.systemDefault())

        val duration = Duration.between(dateTime, now)

        when {
            duration.toMinutes() < 1 -> "baru saja"
            duration.toMinutes() < 60 -> "${duration.toMinutes()} menit yang lalu"
            duration.toHours() < 24 -> "${duration.toHours()} jam yang lalu"
            duration.toDays() < 7 -> "${duration.toDays()} hari yang lalu"
            duration.toDays() < 30 -> "${duration.toDays() / 7} minggu yang lalu"
            duration.toDays() < 365 -> "${duration.toDays() / 30} bulan yang lalu"
            else -> "${duration.toDays() / 365} tahun yang lalu"
        }
    } catch (e: Exception) {
        "Format tidak valid"
    }
}