package com.tt.honeyDue.ui.screens.task

/**
 * Pure-function validators for the task create/edit form.
 *
 * Error strings are kept in 1:1 parity with iOS:
 *  - `iosApp/iosApp/Task/TaskFormView.swift::validateForm()` — the
 *    authoritative source for the "Please select a..." / "... is required"
 *    copy that the user actually sees.
 *  - `iosApp/iosApp/Core/FormStates/TaskFormStates.swift` — container shape.
 *
 * Each function returns `null` when the value is valid, or an error message
 * otherwise. Callers compose these into a [TaskFormErrors] snapshot.
 */
object TaskFormValidation {

    fun validateTitle(value: String): String? =
        if (value.isBlank()) "Title is required" else null

    fun validatePriorityId(value: Int?): String? =
        if (value == null) "Please select a priority" else null

    fun validateCategoryId(value: Int?): String? =
        if (value == null) "Please select a category" else null

    fun validateFrequencyId(value: Int?): String? =
        if (value == null) "Please select a frequency" else null

    /**
     * Estimated cost is optional — an empty/blank string is valid. A non-empty
     * value must parse as a [Double]. Matches iOS `estimatedCost.value.asOptionalDouble`
     * plus the "must be a valid number" phrasing already used for
     * `customIntervalDays` in `TaskFormView.swift`.
     */
    fun validateEstimatedCost(value: String): String? {
        if (value.isBlank()) return null
        return if (value.toDoubleOrNull() == null) {
            "Estimated cost must be a valid number"
        } else {
            null
        }
    }

    fun validateResidenceId(value: Int?): String? =
        if (value == null) "Property is required" else null
}
