package org.timesafari.dailynotification

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class ClearApiSchedulesTest {

    private lateinit var context: Context
    private lateinit var database: DailyNotificationDatabase

    @Before
    fun setUp() {
        context = ApplicationProvider.getApplicationContext()
        database = TestDBFactory.createInMemoryDatabase(context)
    }

    @After
    fun tearDown() {
        database.close()
    }

    @Test
    fun scheduleApiNotification_usesApiIdNamespace() {
        val epochMillis = System.currentTimeMillis() + 300_000
        val expectedId = "${DailyNotificationConstants.API_SCHEDULE_ID_PREFIX}${epochMillis}"

        val scheduled = runBlocking {
            ScheduleHelper.scheduleApiNotification(context, database, epochMillis)
        }

        assertEquals(true, scheduled)
        runBlocking {
            assertNotNull(database.scheduleDao().getById(expectedId))
        }
    }

    @Test
    fun clearApiSchedules_removesOnlyApiManagedAndPreservesDailyReminder() {
        val dailyReminderId = DailyNotificationConstants.DEFAULT_SCHEDULE_ID
        val apiId = "${DailyNotificationConstants.API_SCHEDULE_ID_PREFIX}1740000000000"
        val userScheduleId = "notify_1740000000001"

        runBlocking {
            database.scheduleDao().upsert(
                Schedule(
                    id = dailyReminderId,
                    kind = "notify",
                    clockTime = "08:00",
                    enabled = true,
                    nextRunAt = System.currentTimeMillis() + 60_000
                )
            )
            database.scheduleDao().upsert(
                Schedule(
                    id = apiId,
                    kind = "notify",
                    enabled = true,
                    nextRunAt = System.currentTimeMillis() + 120_000
                )
            )
            database.scheduleDao().upsert(
                Schedule(
                    id = userScheduleId,
                    kind = "notify",
                    enabled = true,
                    nextRunAt = System.currentTimeMillis() + 180_000
                )
            )
        }

        val cleared = runBlocking {
            ScheduleHelper.clearApiSchedules(context, database)
        }

        assertEquals(1, cleared)

        runBlocking {
            assertNotNull(database.scheduleDao().getById(dailyReminderId))
            assertNull(database.scheduleDao().getById(apiId))
            assertNotNull(database.scheduleDao().getById(userScheduleId))
        }
    }
}
