/*
 * Copyright (c) 2026 Meshtastic LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.meshtastic.core.ui.util

import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.v2.runComposeUiTest
import kotlin.test.Test
import kotlin.test.assertTrue

@OptIn(ExperimentalTestApi::class)
class AlertManagerUiTest {

    @Test
    fun alertManager_showsAlert_whenRequested() = runComposeUiTest {
        val alertManager = AlertManager()
        setContent {
            val alertData by alertManager.currentAlert.collectAsState()
            alertData?.let { data -> AlertPreviewRenderer(data) }
        }

        val title = "UI Test Alert"
        val message = "This is a message from a UI test."

        alertManager.showAlert(title = title, message = message)

        onNodeWithText(title).assertIsDisplayed()
        onNodeWithText(message).assertIsDisplayed()
    }

    @Test
    fun alertManager_confirmButton_triggersCallbackAndDismisses() = runComposeUiTest {
        val alertManager = AlertManager()
        var confirmClicked = false
        setContent {
            val alertData by alertManager.currentAlert.collectAsState()
            alertData?.let { data -> AlertPreviewRenderer(data) }
        }

        alertManager.showAlert(title = "Confirm Title", confirmText = "Yes", onConfirm = { confirmClicked = true })

        onNodeWithText("Yes").performClick()

        assertTrue(confirmClicked)
        onNodeWithText("Confirm Title").assertDoesNotExist()
    }
}
