package com.tt.honeyDue.ui.screens

import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import com.tt.honeyDue.testing.AccessibilityIds
import com.tt.honeyDue.ui.BaseScreen

/**
 * Page object for the login screen.
 * Mirrors `iosApp/HoneyDueUITests/PageObjects/LoginScreen.swift`.
 */
class LoginScreen(rule: ComposeTestRule) : BaseScreen(rule) {

    fun enterUsername(value: String): LoginScreen {
        waitFor(AccessibilityIds.Authentication.usernameField)
        tag(AccessibilityIds.Authentication.usernameField).performTextInput(value)
        return this
    }

    fun enterPassword(value: String): LoginScreen {
        waitFor(AccessibilityIds.Authentication.passwordField)
        tag(AccessibilityIds.Authentication.passwordField).performTextInput(value)
        return this
    }

    fun tapLogin(): MainTabScreen {
        waitFor(AccessibilityIds.Authentication.loginButton)
        tag(AccessibilityIds.Authentication.loginButton).performClick()
        return MainTabScreen(rule)
    }

    fun tapSignUp(): RegisterScreen {
        waitFor(AccessibilityIds.Authentication.signUpButton)
        tag(AccessibilityIds.Authentication.signUpButton).performClick()
        return RegisterScreen(rule)
    }

    fun tapForgotPassword(): LoginScreen {
        waitFor(AccessibilityIds.Authentication.forgotPasswordButton)
        tag(AccessibilityIds.Authentication.forgotPasswordButton).performClick()
        return this
    }

    fun togglePasswordVisibility(): LoginScreen {
        waitFor(AccessibilityIds.Authentication.passwordVisibilityToggle)
        tag(AccessibilityIds.Authentication.passwordVisibilityToggle).performClick()
        return this
    }

    /** Convenience: enter credentials and submit. */
    fun login(username: String, password: String): MainTabScreen {
        enterUsername(username)
        enterPassword(password)
        return tapLogin()
    }

    /** Waits for the main tab bar to appear post-login. */
    fun waitForMainTabs(timeoutMs: Long = DEFAULT_TIMEOUT_MS) {
        waitFor(AccessibilityIds.Navigation.residencesTab, timeoutMs)
    }

    override fun isDisplayed(): Boolean = exists(AccessibilityIds.Authentication.usernameField)
}
