package com.rugvedsarika.landarea.logic

object AreaConverter {

    // Base unit is Square Meter (sqm)
    private val unitToSqmMap = mapOf(
        "Square Meter (m²)" to 1.0,
        "Square Foot (ft²)" to 0.092903,
        "Square Yard (yd²)" to 0.836127,
        "Acre" to 4046.856,
        "Hectare" to 10000.0,
        "Are" to 100.0,
        "Cent" to 40.47,
        
        // standard Indian Land Units (Average values)
        "Bigha" to 2529.29,
        "Guntha" to 101.17,
        "Katha" to 126.44,
        "Kanal" to 505.86,
        "Ropani" to 508.74,
        "Marla" to 25.29
    )

    // State-specific Bigha factors (in sqm)
    private val stateBighaFactors = mapOf(
        "Standard" to 2529.29,
        "Rajasthan" to 1618.7,
        "UP (West)" to 2529.29,
        "UP (East)" to 3025.0,
        "Bihar" to 2529.29,
        "West Bengal" to 1337.8,
        "Gujarat" to 1600.0,
        "MP" to 1337.8,
        "Assam" to 1337.8
    )

    fun getAllUnits(): List<String> = unitToSqmMap.keys.toList()
    fun getAllStates(): List<String> = stateBighaFactors.keys.toList()

    fun convert(value: Double, fromUnit: String, toUnit: String, state: String = "Standard"): Double {
        val fromFactor = if (fromUnit == "Bigha") stateBighaFactors[state] ?: 2529.29 else unitToSqmMap[fromUnit] ?: 1.0
        val toFactor = if (toUnit == "Bigha") stateBighaFactors[state] ?: 2529.29 else unitToSqmMap[toUnit] ?: 1.0
        
        // Convert to base (sqm) then to target unit
        val valueInSqm = value * fromFactor
        return valueInSqm / toFactor
    }

    fun calculateCost(area: Double, pricePerUnit: Double): Double {
        return area * pricePerUnit
    }
}
