package com.dano.test1.ui

import android.content.Context

/**
 * Persists dev-only A/B overrides. Not a security boundary; values are for local QA.
 * Use [effectiveVariant] when branching questionnaire or remote-config logic.
 */
object AbTestSettingsStore {
    private const val PREF = "dev_ab_settings"
    private const val KEY_OVERRIDE = "override_enabled"
    private const val KEY_VARIANT = "variant"

    const val VARIANT_NONE = "NONE"
    const val VARIANT_A = "A"
    const val VARIANT_B = "B"

    fun isOverrideEnabled(context: Context): Boolean =
        context.getSharedPreferences(PREF, Context.MODE_PRIVATE).getBoolean(KEY_OVERRIDE, false)

    fun setOverrideEnabled(context: Context, enabled: Boolean) {
        context.getSharedPreferences(PREF, Context.MODE_PRIVATE).edit()
            .putBoolean(KEY_OVERRIDE, enabled)
            .apply()
    }

    fun getVariant(context: Context): String =
        context.getSharedPreferences(PREF, Context.MODE_PRIVATE).getString(KEY_VARIANT, VARIANT_NONE)
            ?: VARIANT_NONE

    fun setVariant(context: Context, variant: String) {
        context.getSharedPreferences(PREF, Context.MODE_PRIVATE).edit()
            .putString(KEY_VARIANT, variant)
            .apply()
    }

    /** Returns "A" or "B" when override is on and a branch is selected; otherwise null. */
    fun effectiveVariant(context: Context): String? {
        if (!isOverrideEnabled(context)) return null
        return when (getVariant(context)) {
            VARIANT_A -> "A"
            VARIANT_B -> "B"
            else -> null
        }
    }
}
