/*
 * Copyright 2026 Signal Messenger, LLC
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package org.signal.registration.screens.discoverability

import android.app.Application
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ApplicationProvider
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.signal.core.ui.CoreUiDependenciesRule
import org.signal.core.ui.compose.theme.SignalTheme
import org.signal.registration.test.TestTags

/**
 * Tests for PhoneNumberDiscoverabilityScreen that validate event emissions.
 * Uses Robolectric to run fast JUnit tests without an emulator.
 */
@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class PhoneNumberDiscoverabilityScreenTest {

  @get:Rule
  val composeTestRule = createComposeRule()

  @get:Rule
  val coreUiDependenciesRule = CoreUiDependenciesRule(ApplicationProvider.getApplicationContext())

  @Test
  fun `screen displays everyone and nobody options and save button`() {
    // Given
    composeTestRule.setContent {
      SignalTheme {
        PhoneNumberDiscoverabilityScreen(
          state = PhoneNumberDiscoverabilityState(),
          onEvent = {}
        )
      }
    }

    // Then
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_EVERYONE_OPTION).assertIsDisplayed()
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_NOBODY_OPTION).assertIsDisplayed()
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_SAVE_BUTTON).assertIsDisplayed()
  }

  @Test
  fun `when nobody option is clicked, NobodySelected event is emitted`() {
    // Given
    var emittedEvent: PhoneNumberDiscoverabilityScreenEvents? = null

    composeTestRule.setContent {
      SignalTheme {
        PhoneNumberDiscoverabilityScreen(
          state = PhoneNumberDiscoverabilityState(),
          onEvent = { event ->
            emittedEvent = event
          }
        )
      }
    }

    // When
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_NOBODY_OPTION).performClick()

    // Then
    assert(emittedEvent == PhoneNumberDiscoverabilityScreenEvents.NobodySelected)
  }

  @Test
  fun `when everyone option is clicked, EveryoneSelected event is emitted`() {
    // Given
    var emittedEvent: PhoneNumberDiscoverabilityScreenEvents? = null

    composeTestRule.setContent {
      SignalTheme {
        PhoneNumberDiscoverabilityScreen(
          state = PhoneNumberDiscoverabilityState(discoverable = false),
          onEvent = { event ->
            emittedEvent = event
          }
        )
      }
    }

    // When
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_EVERYONE_OPTION).performClick()

    // Then
    assert(emittedEvent == PhoneNumberDiscoverabilityScreenEvents.EveryoneSelected)
  }

  @Test
  fun `when save button is clicked, SaveClicked event is emitted`() {
    // Given
    var emittedEvent: PhoneNumberDiscoverabilityScreenEvents? = null

    composeTestRule.setContent {
      SignalTheme {
        PhoneNumberDiscoverabilityScreen(
          state = PhoneNumberDiscoverabilityState(),
          onEvent = { event ->
            emittedEvent = event
          }
        )
      }
    }

    // When
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_SAVE_BUTTON).performClick()

    // Then
    assert(emittedEvent == PhoneNumberDiscoverabilityScreenEvents.SaveClicked)
  }

  @Test
  fun `when back button is clicked, BackClicked event is emitted`() {
    // Given
    var emittedEvent: PhoneNumberDiscoverabilityScreenEvents? = null

    composeTestRule.setContent {
      SignalTheme {
        PhoneNumberDiscoverabilityScreen(
          state = PhoneNumberDiscoverabilityState(),
          onEvent = { event ->
            emittedEvent = event
          }
        )
      }
    }

    // When
    composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_DISCOVERABILITY_BACK_BUTTON).performClick()

    // Then
    assert(emittedEvent == PhoneNumberDiscoverabilityScreenEvents.BackClicked)
  }
}
