package com.tt.honeyDue.ui.screens.residence

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

/**
 * Pure-function validator tests for [ResidenceFormValidation].
 *
 * Rules are derived from iOS:
 *  - `iosApp/iosApp/ResidenceFormView.swift::validateForm()` — only `name`
 *    is required; `canSave: !name.isEmpty`.
 *  - `iosApp/iosApp/Core/FormStates/ResidenceFormState.swift` — numeric
 *    fields (bedrooms, bathrooms, squareFootage, lotSize, yearBuilt) use
 *    `asOptionalInt`/`asOptionalDouble` — empty is valid, non-numeric
 *    silently returns nil. We surface that as a user-facing error here so
 *    the KMM client can tell the user their input was dropped rather than
 *    failing silently.
 *
 * Error copy uses the same phrasing style as Stream W's
 * `TaskFormValidation` ("... is required", "... must be a valid number").
 */
class ResidenceFormValidationTest {

    // --- validateName ------------------------------------------------------

    @Test
    fun validateName_emptyString_returnsNameRequired() {
        assertEquals("Name is required", ResidenceFormValidation.validateName(""))
    }

    @Test
    fun validateName_blankString_returnsNameRequired() {
        assertEquals("Name is required", ResidenceFormValidation.validateName("   "))
    }

    @Test
    fun validateName_nonEmpty_returnsNull() {
        assertNull(ResidenceFormValidation.validateName("Home"))
    }

    @Test
    fun validateName_atMaxLength_returnsNull() {
        assertNull(ResidenceFormValidation.validateName("a".repeat(100)))
    }

    @Test
    fun validateName_overMaxLength_returnsError() {
        assertEquals(
            "Name must be 100 characters or fewer",
            ResidenceFormValidation.validateName("a".repeat(101)),
        )
    }

    // --- validateBedrooms --------------------------------------------------

    @Test
    fun validateBedrooms_empty_returnsNull() {
        assertNull(ResidenceFormValidation.validateBedrooms(""))
    }

    @Test
    fun validateBedrooms_validInteger_returnsNull() {
        assertNull(ResidenceFormValidation.validateBedrooms("3"))
    }

    @Test
    fun validateBedrooms_zero_returnsNull() {
        // iOS `asOptionalInt` accepts 0. Match that behaviour.
        assertNull(ResidenceFormValidation.validateBedrooms("0"))
    }

    @Test
    fun validateBedrooms_negative_returnsError() {
        assertEquals(
            "Bedrooms must be a non-negative whole number",
            ResidenceFormValidation.validateBedrooms("-1"),
        )
    }

    @Test
    fun validateBedrooms_decimal_returnsError() {
        assertEquals(
            "Bedrooms must be a non-negative whole number",
            ResidenceFormValidation.validateBedrooms("2.5"),
        )
    }

    @Test
    fun validateBedrooms_nonNumeric_returnsError() {
        assertEquals(
            "Bedrooms must be a non-negative whole number",
            ResidenceFormValidation.validateBedrooms("abc"),
        )
    }

    // --- validateBathrooms -------------------------------------------------

    @Test
    fun validateBathrooms_empty_returnsNull() {
        assertNull(ResidenceFormValidation.validateBathrooms(""))
    }

    @Test
    fun validateBathrooms_validDecimal_returnsNull() {
        assertNull(ResidenceFormValidation.validateBathrooms("2.5"))
    }

    @Test
    fun validateBathrooms_validInteger_returnsNull() {
        assertNull(ResidenceFormValidation.validateBathrooms("3"))
    }

    @Test
    fun validateBathrooms_negative_returnsError() {
        assertEquals(
            "Bathrooms must be a non-negative number",
            ResidenceFormValidation.validateBathrooms("-0.5"),
        )
    }

    @Test
    fun validateBathrooms_nonNumeric_returnsError() {
        assertEquals(
            "Bathrooms must be a non-negative number",
            ResidenceFormValidation.validateBathrooms("abc"),
        )
    }

    // --- validateSquareFootage --------------------------------------------

    @Test
    fun validateSquareFootage_empty_returnsNull() {
        assertNull(ResidenceFormValidation.validateSquareFootage(""))
    }

    @Test
    fun validateSquareFootage_validInteger_returnsNull() {
        assertNull(ResidenceFormValidation.validateSquareFootage("1500"))
    }

    @Test
    fun validateSquareFootage_zero_returnsError() {
        assertEquals(
            "Square footage must be a positive whole number",
            ResidenceFormValidation.validateSquareFootage("0"),
        )
    }

    @Test
    fun validateSquareFootage_negative_returnsError() {
        assertEquals(
            "Square footage must be a positive whole number",
            ResidenceFormValidation.validateSquareFootage("-100"),
        )
    }

    @Test
    fun validateSquareFootage_nonNumeric_returnsError() {
        assertEquals(
            "Square footage must be a positive whole number",
            ResidenceFormValidation.validateSquareFootage("big"),
        )
    }

    // --- validateLotSize ---------------------------------------------------

    @Test
    fun validateLotSize_empty_returnsNull() {
        assertNull(ResidenceFormValidation.validateLotSize(""))
    }

    @Test
    fun validateLotSize_validDecimal_returnsNull() {
        assertNull(ResidenceFormValidation.validateLotSize("0.25"))
    }

    @Test
    fun validateLotSize_negative_returnsError() {
        assertEquals(
            "Lot size must be a positive number",
            ResidenceFormValidation.validateLotSize("-1"),
        )
    }

    @Test
    fun validateLotSize_zero_returnsError() {
        assertEquals(
            "Lot size must be a positive number",
            ResidenceFormValidation.validateLotSize("0"),
        )
    }

    @Test
    fun validateLotSize_nonNumeric_returnsError() {
        assertEquals(
            "Lot size must be a positive number",
            ResidenceFormValidation.validateLotSize("huge"),
        )
    }

    // --- validateYearBuilt -------------------------------------------------

    @Test
    fun validateYearBuilt_empty_returnsNull() {
        assertNull(ResidenceFormValidation.validateYearBuilt(""))
    }

    @Test
    fun validateYearBuilt_validYear_returnsNull() {
        assertNull(ResidenceFormValidation.validateYearBuilt("1998"))
    }

    @Test
    fun validateYearBuilt_earliestValid_returnsNull() {
        assertNull(ResidenceFormValidation.validateYearBuilt("1800"))
    }

    @Test
    fun validateYearBuilt_beforeMinimum_returnsError() {
        assertEquals(
            "Year built must be between 1800 and the current year",
            ResidenceFormValidation.validateYearBuilt("1799"),
        )
    }

    @Test
    fun validateYearBuilt_wrongDigitCount_returnsError() {
        assertEquals(
            "Year built must be a 4-digit year",
            ResidenceFormValidation.validateYearBuilt("98"),
        )
    }

    @Test
    fun validateYearBuilt_nonNumeric_returnsError() {
        assertEquals(
            "Year built must be a 4-digit year",
            ResidenceFormValidation.validateYearBuilt("abcd"),
        )
    }
}
