package com.tt.honeyDue.widget

import kotlin.math.abs

/**
 * Platform-agnostic helpers that format widget strings to match iOS exactly.
 *
 * The corresponding Swift implementation lives in
 * `iosApp/HoneyDue/HoneyDue.swift` (see `formatWidgetDate(_:)` and the
 * inline labels in `FreeWidgetView`/`SmallWidgetView`/`MediumWidgetView`).
 * Any behavioral change here must be reflected on iOS and vice versa,
 * otherwise the two platforms ship visually different widgets.
 *
 * ## Formatter parity contract
 *
 *  - `formatDueDateRelative(0)`   → `"Today"`
 *  - `formatDueDateRelative(1)`   → `"in 1 day"`
 *  - `formatDueDateRelative(n>1)` → `"in N days"`
 *  - `formatDueDateRelative(-1)`  → `"1 day ago"`
 *  - `formatDueDateRelative(-n)`  → `"N days ago"`
 *
 * The shared `WidgetTaskDto` pre-computes `daysUntilDue` on the server
 * side, so this function takes that offset directly rather than parsing
 * the dueDate string client-side.
 */
object WidgetFormatter {

    /**
     * Render a short relative due-date description. See the class doc for
     * the parity contract.
     */
    fun formatDueDateRelative(daysUntilDue: Int): String {
        if (daysUntilDue == 0) return "Today"
        if (daysUntilDue > 0) {
            return if (daysUntilDue == 1) "in 1 day" else "in $daysUntilDue days"
        }
        val ago = abs(daysUntilDue)
        return if (ago == 1) "1 day ago" else "$ago days ago"
    }

    /**
     * Long label under the count on the free-tier widget, matching iOS
     * `FreeWidgetView`: "task waiting" / "tasks waiting".
     */
    fun taskCountLabel(count: Int): String =
        if (count == 1) "task waiting" else "tasks waiting"

    /**
     * Short label used by SmallWidgetView/MediumWidgetView under the big
     * number. iOS: "task" / "tasks".
     */
    fun compactTaskCountLabel(count: Int): String =
        if (count == 1) "task" else "tasks"
}
