package xyz.kusoft.tradebotmonitor

import android.content.Context

class AppPrefs(context: Context) {
    private val prefs = context.getSharedPreferences("tradebot_monitor", Context.MODE_PRIVATE)

    init {
        val saved = prefs.getString("api_base_url", null)?.trim()
        if (saved.isNullOrBlank() || saved == LEGACY_PI_API_BASE_URL) {
            prefs.edit().putString("api_base_url", DEFAULT_API_BASE_URL).apply()
        }
        if (prefs.getString("training_computer_name", null).isNullOrBlank()) {
            pinDefaultTrainingComputer()
        }
    }

    var apiBaseUrl: String
        get() = normalizeBaseUrl(prefs.getString("api_base_url", DEFAULT_API_BASE_URL) ?: DEFAULT_API_BASE_URL)
        set(value) = prefs.edit().putString("api_base_url", normalizeBaseUrl(value)).apply()

    var commandToken: String
        get() = prefs.getString("command_token", "") ?: ""
        set(value) = prefs.edit().putString("command_token", value.trim()).apply()

    var selectedSymbol: String
        get() = prefs.getString("selected_symbol", "BTCUSDT") ?: "BTCUSDT"
        set(value) = prefs.edit().putString("selected_symbol", value).apply()

    var themeMode: String
        get() = prefs.getString("theme_mode", "dark") ?: "dark"
        set(value) = prefs.edit().putString("theme_mode", value).apply()

    var retrainScheduleEnabled: Boolean
        get() = prefs.getBoolean("retrain_schedule_enabled", false)
        set(value) = prefs.edit().putBoolean("retrain_schedule_enabled", value).apply()

    var retrainIntervalHours: Int
        get() = prefs.getInt("retrain_interval_hours", 6)
        set(value) = prefs.edit().putInt("retrain_interval_hours", value.coerceAtLeast(1)).apply()

    val trainingComputerName: String
        get() = prefs.getString("training_computer_name", DEFAULT_TRAINING_COMPUTER_NAME) ?: DEFAULT_TRAINING_COMPUTER_NAME

    val trainingComputerPath: String
        get() = prefs.getString("training_computer_path", DEFAULT_TRAINING_COMPUTER_PATH) ?: DEFAULT_TRAINING_COMPUTER_PATH

    val trainingComputerPinned: Boolean
        get() = prefs.getBoolean("training_computer_pinned", true)

    fun pinDefaultTrainingComputer() {
        prefs.edit()
            .putString("training_computer_name", DEFAULT_TRAINING_COMPUTER_NAME)
            .putString("training_computer_path", DEFAULT_TRAINING_COMPUTER_PATH)
            .putBoolean("training_computer_pinned", true)
            .apply()
    }

    private fun normalizeBaseUrl(value: String): String {
        val trimmed = value.trim().trimEnd('/')
        if (trimmed.isBlank()) return DEFAULT_API_BASE_URL
        return if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
            trimmed
        } else {
            "https://$trimmed"
        }
    }

    private companion object {
        const val DEFAULT_API_BASE_URL = "https://tb.kusoft.xyz"
        const val LEGACY_PI_API_BASE_URL = "http://192.168.0.185:8787"
        const val DEFAULT_TRAINING_COMPUTER_NAME = "DESKTOP-TMFDL0H"
        const val DEFAULT_TRAINING_COMPUTER_PATH = "C:\\Repos\\TradeBot"
    }
}
