package com.swoosh.microblog.data

import android.content.Context
import android.content.SharedPreferences
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = android.app.Application::class)
class NewsletterPreferencesTest {

    private lateinit var prefs: SharedPreferences
    private lateinit var newsletterPreferences: NewsletterPreferences

    private val testAccountId = "test-account-123"

    @Before
    fun setup() {
        val context = RuntimeEnvironment.getApplication()
        prefs = context.getSharedPreferences(NewsletterPreferences.PREFS_NAME, Context.MODE_PRIVATE)
        prefs.edit().clear().commit()
        newsletterPreferences = NewsletterPreferences(prefs, testAccountId)
    }

    // --- Default values ---

    @Test
    fun `default newsletter enabled is false`() {
        assertFalse(newsletterPreferences.isNewsletterEnabled())
    }

    // --- Setting and getting ---

    @Test
    fun `setting newsletter enabled to true persists`() {
        newsletterPreferences.setNewsletterEnabled(true)
        assertTrue(newsletterPreferences.isNewsletterEnabled())
    }

    @Test
    fun `setting newsletter enabled to false persists`() {
        newsletterPreferences.setNewsletterEnabled(true)
        newsletterPreferences.setNewsletterEnabled(false)
        assertFalse(newsletterPreferences.isNewsletterEnabled())
    }

    @Test
    fun `newsletter enabled persists across instances`() {
        newsletterPreferences.setNewsletterEnabled(true)
        val newInstance = NewsletterPreferences(prefs, testAccountId)
        assertTrue(newInstance.isNewsletterEnabled())
    }

    @Test
    fun `toggling on then off round-trips correctly`() {
        newsletterPreferences.setNewsletterEnabled(true)
        assertTrue(newsletterPreferences.isNewsletterEnabled())
        newsletterPreferences.setNewsletterEnabled(false)
        assertFalse(newsletterPreferences.isNewsletterEnabled())
    }

    // --- Per-account isolation ---

    @Test
    fun `different accounts have independent newsletter settings`() {
        val prefs1 = NewsletterPreferences(prefs, "account-1")
        val prefs2 = NewsletterPreferences(prefs, "account-2")

        prefs1.setNewsletterEnabled(true)
        prefs2.setNewsletterEnabled(false)

        assertTrue(prefs1.isNewsletterEnabled())
        assertFalse(prefs2.isNewsletterEnabled())
    }

    @Test
    fun `enabling for one account does not affect another`() {
        val prefs1 = NewsletterPreferences(prefs, "account-a")
        val prefs2 = NewsletterPreferences(prefs, "account-b")

        prefs1.setNewsletterEnabled(true)

        assertTrue(prefs1.isNewsletterEnabled())
        assertFalse(prefs2.isNewsletterEnabled())
    }

    @Test
    fun `empty account id still works`() {
        val emptyPrefs = NewsletterPreferences(prefs, "")
        emptyPrefs.setNewsletterEnabled(true)
        assertTrue(emptyPrefs.isNewsletterEnabled())
    }
}
