package com.jordanbot.autoride.filter

import android.content.Context
import android.content.SharedPreferences
import com.jordanbot.autoride.model.RideRequest

object FilterEngine {
    
    private const val PREFS_NAME = "BotSettings"
    private lateinit var prefs: SharedPreferences

    var minPriceJod: Double = 1.0
    var maxMinutesAway: Int = 10
    var isEnabled: Boolean = true
    
    var enableUber: Boolean = true
    var enableCareem: Boolean = true
    var enableJeeny: Boolean = true

    fun init(context: Context) {
        prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
        load()
    }

    fun load() {
        minPriceJod = prefs.getFloat("min_price", 1.0f).toDouble()
        maxMinutesAway = prefs.getInt("max_minutes", 10)
        isEnabled = prefs.getBoolean("auto_accept", true)
        enableUber = prefs.getBoolean("enable_uber", true)
        enableCareem = prefs.getBoolean("enable_careem", true)
        enableJeeny = prefs.getBoolean("enable_jeeny", true)
    }

    fun save(price: Double, minutes: Int, autoAccept: Boolean, uber: Boolean, careem: Boolean, jeeny: Boolean) {
        prefs.edit().apply {
            putFloat("min_price", price.toFloat())
            putInt("max_minutes", minutes)
            putBoolean("auto_accept", autoAccept)
            putBoolean("enable_uber", uber)
            putBoolean("enable_careem", careem)
            putBoolean("enable_jeeny", jeeny)
            apply()
        }
        load()
    }

    fun evaluate(request: RideRequest): Boolean {
        if (!isEnabled) return false

        // Check app package
        val pkg = request.appPackage
        if (pkg.contains("uber") && !enableUber) return false
        if (pkg.contains("careem") && !enableCareem) return false
        if (pkg.contains("jeeny") && !enableJeeny) return false

        // Check Price
        if (request.priceJod != null && request.priceJod < minPriceJod) {
            return false
        }

        // Check Distance/Time
        if (request.minutesAway != null && request.minutesAway > maxMinutesAway) {
            return false
        }

        // Passes all filters
        return true
    }
}
