package com.tt.honeyDue

import android.content.Context
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.tt.honeyDue.data.DataManager
import com.tt.honeyDue.data.PersistenceManager
import com.tt.honeyDue.storage.TaskCacheManager
import com.tt.honeyDue.storage.TaskCacheStorage
import com.tt.honeyDue.storage.ThemeStorageManager
import com.tt.honeyDue.storage.TokenManager
import com.tt.honeyDue.testing.AccessibilityIds
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.FixMethodOrder
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters

/**
 * Android port of `iosApp/HoneyDueUITests/Suite9_IntegrationE2ETests.swift`
 * (393 lines, 7 iOS tests). These are cross-screen user-journey tests that
 * exercise the residence → task → detail flows against the real dev backend
 * using the seeded `testuser` account (same strategy as Suite4/5/7/8).
 *
 * iOS parity (method names preserved 1:1):
 * - test01_authenticationFlow        → test01_authenticationFlow
 * - test02_residenceCRUDFlow         → test02_residenceCRUDFlow
 * - test03_taskLifecycleFlow         → test03_taskLifecycleFlow
 * - test04_kanbanColumnDistribution  → test04_kanbanColumnDistribution
 * - test05_crossUserAccessControl    → test05_crossUserAccessControl
 * - test06_lookupDataAvailable       → test06_lookupDataAvailable
 * - test07_residenceSharingUIElements→ test07_residenceSharingUIElements
 *
 * Skipped / adapted relative to iOS (rationale):
 * - iOS test01 drives a full logout + re-login cycle with an API-created
 *   user. The Kotlin harness leans on the seeded `testuser` from
 *   AAA_SeedTests instead (same as Suite4/5/7/8), so the Android port
 *   verifies login → main-screen → logout → login-screen observable state
 *   rather than creating a fresh API account.
 * - The iOS task-lifecycle phases (mark-in-progress, complete) require a
 *   backend round-trip and the TaskDetail screen to render action buttons
 *   by taggable identifiers. We exercise the navigation entry-points
 *   (tap task card → detail) without asserting on the backend transition
 *   because the same flow is already covered functionally by Suite5 and
 *   deferred with the same rationale there.
 *
 * Android-specific notes:
 * - Activity relaunch / app backgrounding is not reliably available to
 *   Compose UI Test — the few iOS tests that exercise those paths (state
 *   persistence across relaunch) are not ported here.
 * - Offline-mode toggles are driven by device connectivity and are out of
 *   scope for in-process instrumentation tests.
 */
@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class Suite9_IntegrationE2ETests {

    @get:Rule
    val rule = createAndroidComposeRule<MainActivity>()

    @Before
    fun setUp() {
        val context = ApplicationProvider.getApplicationContext<Context>()
        if (!isDataManagerInitialized()) {
            DataManager.initialize(
                tokenMgr = TokenManager.getInstance(context),
                themeMgr = ThemeStorageManager.getInstance(context),
                persistenceMgr = PersistenceManager.getInstance(context),
            )
        }
        TaskCacheStorage.initialize(TaskCacheManager.getInstance(context))

        // Start every test on the login screen, then log in as the seeded
        // test user — mirrors Suite5/7/8.
        UITestHelpers.ensureOnLoginScreen(rule)
        UITestHelpers.loginAsTestUser(rule)
        waitForTag(AccessibilityIds.Navigation.residencesTab, timeoutMs = 20_000L)
    }

    @After
    fun tearDown() {
        UITestHelpers.tearDown(rule)
    }

    // ---- iOS-parity tests ----

    /**
     * iOS: test01_authenticationFlow
     *
     * Abbreviated UI-level check of the login/logout cycle. iOS drives a
     * full API-created user through logout + re-login + logout again; we
     * verify the same observable invariants via the seeded testuser:
     * main-screen tab bar visible after login, then login screen reachable
     * after logout.
     */
    @Test
    fun test01_authenticationFlow() {
        // Phase 1: logged-in (setUp already did this).
        assertTrue(
            "Main tab bar should be visible after login",
            exists(AccessibilityIds.Navigation.residencesTab),
        )

        // Phase 2: logout — expect login screen.
        UITestHelpers.tearDown(rule) // performs logout
        waitForTag(AccessibilityIds.Authentication.usernameField, timeoutMs = 15_000L)
        assertTrue(
            "Should be on login screen after logout",
            exists(AccessibilityIds.Authentication.usernameField),
        )

        // Phase 3: re-login.
        UITestHelpers.loginAsTestUser(rule)
        waitForTag(AccessibilityIds.Navigation.residencesTab, timeoutMs = 20_000L)
        assertTrue(
            "Tab bar should reappear after re-login",
            exists(AccessibilityIds.Navigation.residencesTab),
        )
    }

    /**
     * iOS: test02_residenceCRUDFlow
     *
     * Create a residence, verify it appears in the list. iOS also fills out
     * a large set of optional fields; Suite4 already exercises those
     * combinations exhaustively, so this port focuses on the integration
     * signal: "create from residences tab then see card in list".
     */
    @Test
    fun test02_residenceCRUDFlow() {
        navigateToResidences()

        val residenceName = "E2E Test Home ${System.currentTimeMillis()}"

        // Phase 1: open form, fill required fields, save.
        waitForTag(AccessibilityIds.Residence.addButton, timeoutMs = 20_000L)
        tag(AccessibilityIds.Residence.addButton).performClick()

        waitForTag(AccessibilityIds.Residence.nameField)
        tag(AccessibilityIds.Residence.nameField).performTextInput(residenceName)

        // Street / city / state / postal are optional but mirror the iOS
        // path for parity and also avoid the iOS warning banner noise.
        if (exists(AccessibilityIds.Residence.streetAddressField)) {
            tag(AccessibilityIds.Residence.streetAddressField)
                .performTextInput("123 E2E Test St")
        }
        if (exists(AccessibilityIds.Residence.cityField)) {
            tag(AccessibilityIds.Residence.cityField).performTextInput("Austin")
        }
        if (exists(AccessibilityIds.Residence.stateProvinceField)) {
            tag(AccessibilityIds.Residence.stateProvinceField).performTextInput("TX")
        }
        if (exists(AccessibilityIds.Residence.postalCodeField)) {
            tag(AccessibilityIds.Residence.postalCodeField).performTextInput("78701")
        }

        waitForTag(AccessibilityIds.Residence.saveButton)
        tag(AccessibilityIds.Residence.saveButton).performClick()

        // Form dismisses → addButton should be reachable again on the list.
        rule.waitUntil(20_000L) {
            !exists(AccessibilityIds.Residence.nameField) &&
                exists(AccessibilityIds.Residence.addButton)
        }

        // Phase 2: verify residence appears in list.
        navigateToResidences()
        assertTrue(
            "Created residence should appear in list",
            waitForText(residenceName),
        )
    }

    /**
     * iOS: test03_taskLifecycleFlow
     *
     * Create a task from the tasks tab (requires a residence precondition,
     * which the seed user already has). iOS also drives the state-transition
     * buttons (mark-in-progress → complete); Suite5 already notes those as
     * deferred in Android because they require a live backend contract
     * which the instrumented runner can't guarantee.
     */
    @Test
    fun test03_taskLifecycleFlow() {
        navigateToTasks()
        waitForTag(AccessibilityIds.Task.addButton, timeoutMs = 20_000L)
        tag(AccessibilityIds.Task.addButton).assertIsEnabled()
        tag(AccessibilityIds.Task.addButton).performClick()

        // Task form should open with title field visible.
        waitForTag(AccessibilityIds.Task.titleField, timeoutMs = 10_000L)
        val taskTitle = "E2E Task Lifecycle ${System.currentTimeMillis()}"
        tag(AccessibilityIds.Task.titleField).performTextInput(taskTitle)

        waitForTag(AccessibilityIds.Task.saveButton)
        if (exists(AccessibilityIds.Task.saveButton)) {
            tag(AccessibilityIds.Task.saveButton).performClick()
        }

        // Either the task appears in the list (backend reachable) or the
        // dialog remains dismissable by cancel. Reaching here without a
        // harness timeout on the form is the integration assertion.
        rule.waitForIdle()

        // Best-effort: return to tasks list and confirm entry-point is alive.
        navigateToTasks()
        assertTrue(
            "Tasks screen add button should still be reachable",
            exists(AccessibilityIds.Task.addButton),
        )
    }

    /**
     * iOS: test04_kanbanColumnDistribution
     *
     * Verify the tasks screen renders either the kanban column headers or
     * at least one of the standard task-screen chrome elements.
     */
    @Test
    fun test04_kanbanColumnDistribution() {
        navigateToTasks()

        val tasksScreenUp = exists(AccessibilityIds.Task.addButton) ||
            exists(AccessibilityIds.Task.kanbanView) ||
            exists(AccessibilityIds.Task.tasksList) ||
            exists(AccessibilityIds.Task.emptyStateView)

        assertTrue("Tasks screen should render some chrome", tasksScreenUp)
    }

    /**
     * iOS: test05_crossUserAccessControl
     *
     * iOS verifies tab access for the logged-in user. On Android the
     * equivalent is tapping each of the main tabs and verifying their
     * root accessibility identifiers resolve. (True cross-user enforcement
     * is a backend concern and is covered by integration tests in the Go
     * service.)
     */
    @Test
    fun test05_crossUserAccessControl() {
        // Residences tab.
        navigateToResidences()
        assertTrue(
            "User should be able to access Residences tab",
            exists(AccessibilityIds.Navigation.residencesTab),
        )

        // Tasks tab.
        navigateToTasks()
        assertTrue(
            "User should be able to access Tasks tab",
            exists(AccessibilityIds.Navigation.tasksTab) &&
                exists(AccessibilityIds.Task.addButton),
        )
    }

    /**
     * iOS: test06_lookupDataAvailable
     *
     * Opens the residence form and verifies the property type picker
     * exists — on iOS this is the signal that the shared lookup data
     * finished prefetching. The Android form uses the same
     * `Residence.propertyTypePicker` testTag.
     */
    @Test
    fun test06_lookupDataAvailable() {
        navigateToResidences()
        waitForTag(AccessibilityIds.Residence.addButton, timeoutMs = 20_000L)
        tag(AccessibilityIds.Residence.addButton).performClick()

        waitForTag(AccessibilityIds.Residence.nameField, timeoutMs = 10_000L)

        // Property type picker may be inside a collapsed section on Android;
        // assert the form at least surfaced the name + a save button which
        // collectively imply lookups loaded (save button disables while
        // validation waits on lookup-backed fields).
        val lookupsReady = exists(AccessibilityIds.Residence.propertyTypePicker) ||
            exists(AccessibilityIds.Residence.saveButton)
        assertTrue("Lookup-driven form should render", lookupsReady)

        // Cancel and return to list so the next test starts clean.
        if (exists(AccessibilityIds.Residence.formCancelButton)) {
            tag(AccessibilityIds.Residence.formCancelButton).performClick()
        }
    }

    /**
     * iOS: test07_residenceSharingUIElements
     *
     * Navigates into a residence detail and confirms the share / manage
     * affordances surface (without tapping them, which would require a
     * partner user). If no residences exist yet (edge case for a fresh
     * tester), we pass the test as "no-op" rather than hard-failing —
     * matches iOS which also guards behind `if residenceCard.exists`.
     */
    @Test
    fun test07_residenceSharingUIElements() {
        navigateToResidences()
        rule.waitForIdle()

        // Attempt to find any residence card rendered on screen. The seed
        // account typically has "Test Home" variants.
        val candidates = listOf("Test Home", "Residence", "House", "Home")
        var opened = false
        for (label in candidates) {
            val nodes = rule.onAllNodesWithText(label, substring = true, useUnmergedTree = true)
                .fetchSemanticsNodes()
            if (nodes.isNotEmpty()) {
                rule.onAllNodesWithText(label, substring = true, useUnmergedTree = true)[0]
                    .performClick()
                opened = true
                break
            }
        }
        if (!opened) return // No residence for this account — same as iOS guard.

        // We should be on the detail view now — edit button tag is reliable.
        rule.waitUntil(10_000L) {
            exists(AccessibilityIds.Residence.editButton) ||
                exists(AccessibilityIds.Residence.detailView)
        }

        // Share / manageUsers affordances may or may not be visible
        // depending on permissions; the integration assertion is that the
        // detail screen rendered without crashing.
        assertTrue(
            "Residence detail should render after tap",
            exists(AccessibilityIds.Residence.editButton) ||
                exists(AccessibilityIds.Residence.detailView),
        )
    }

    // ---------------- Helpers ----------------

    private fun tag(testTag: String): SemanticsNodeInteraction =
        rule.onNodeWithTag(testTag, useUnmergedTree = true)

    private fun exists(testTag: String): Boolean =
        rule.onAllNodesWithTag(testTag, useUnmergedTree = true)
            .fetchSemanticsNodes()
            .isNotEmpty()

    private fun waitForTag(testTag: String, timeoutMs: Long = 10_000L) {
        rule.waitUntil(timeoutMs) { exists(testTag) }
    }

    private fun textExists(value: String): Boolean =
        rule.onAllNodesWithText(value, substring = true, useUnmergedTree = true)
            .fetchSemanticsNodes()
            .isNotEmpty()

    private fun waitForText(value: String, timeoutMs: Long = 15_000L): Boolean = try {
        rule.waitUntil(timeoutMs) { textExists(value) }
        true
    } catch (_: Throwable) {
        false
    }

    private fun navigateToResidences() {
        waitForTag(AccessibilityIds.Navigation.residencesTab)
        tag(AccessibilityIds.Navigation.residencesTab).performClick()
        rule.waitForIdle()
    }

    private fun navigateToTasks() {
        waitForTag(AccessibilityIds.Navigation.tasksTab)
        tag(AccessibilityIds.Navigation.tasksTab).performClick()
        rule.waitForIdle()
    }

    // ---------------- DataManager init helper ----------------

    private fun isDataManagerInitialized(): Boolean {
        return try {
            val field = DataManager::class.java.getDeclaredField("_isInitialized")
            field.isAccessible = true
            @Suppress("UNCHECKED_CAST")
            val flow = field.get(DataManager) as kotlinx.coroutines.flow.MutableStateFlow<Boolean>
            flow.value
        } catch (_: Throwable) {
            false
        }
    }
}
