package com.rssuper.models

import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test

class NotificationPreferencesTest {

    private lateinit var moshi: Moshi
    private lateinit var adapter: com.squareup.moshi.JsonAdapter<NotificationPreferences>

    @Before
    fun setup() {
        moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()
        adapter = moshi.adapter(NotificationPreferences::class.java)
    }

    @Test
    fun testSerialization() {
        val preferences = NotificationPreferences(
            newArticles = true,
            episodeReleases = true,
            customAlerts = false,
            badgeCount = true,
            sound = true,
            vibration = false
        )

        val json = adapter.toJson(preferences)
        assertNotNull(json)
    }

    @Test
    fun testDeserialization() {
        val json = """{
            "newArticles": true,
            "episodeReleases": true,
            "customAlerts": false,
            "badgeCount": true,
            "sound": true,
            "vibration": false
        }"""

        val preferences = adapter.fromJson(json)
        assertNotNull(preferences)
        assertEquals(true, preferences?.newArticles)
        assertEquals(true, preferences?.episodeReleases)
        assertEquals(false, preferences?.customAlerts)
        assertEquals(true, preferences?.badgeCount)
        assertEquals(true, preferences?.sound)
        assertEquals(false, preferences?.vibration)
    }

    @Test
    fun testDefaultValues() {
        val preferences = NotificationPreferences()

        assertEquals(true, preferences.newArticles)
        assertEquals(true, preferences.episodeReleases)
        assertEquals(false, preferences.customAlerts)
        assertEquals(true, preferences.badgeCount)
        assertEquals(true, preferences.sound)
        assertEquals(true, preferences.vibration)
    }

    @Test
    fun testCopy() {
        val original = NotificationPreferences(
            newArticles = true,
            sound = true
        )

        val modified = original.copy(newArticles = false, sound = false)

        assertEquals(false, modified.newArticles)
        assertEquals(false, modified.sound)
        assertEquals(true, modified.episodeReleases)
    }

    @Test
    fun testEqualsAndHashCode() {
        val pref1 = NotificationPreferences(newArticles = true, sound = true)
        val pref2 = NotificationPreferences(newArticles = true, sound = true)
        val pref3 = NotificationPreferences(newArticles = false, sound = true)

        assertEquals(pref1, pref2)
        assertEquals(pref1.hashCode(), pref2.hashCode())
        assert(pref1 != pref3)
    }

    @Test
    fun testToString() {
        val preferences = NotificationPreferences(
            newArticles = true,
            sound = true
        )

        val toString = preferences.toString()
        assertNotNull(toString)
        assert(toString.contains("newArticles"))
        assert(toString.contains("sound"))
    }
}
