//
//  OnboardingScreen.swift
//  Tests iOS
//
//  Screen object for the onboarding flow — welcome, time, day, style, and subscription screens.
//

import XCTest

struct OnboardingScreen {
    let app: XCUIApplication

    // MARK: - Screen Elements

    var welcomeScreen: XCUIElement { app.element(UITestID.Onboarding.welcome) }
    var timeScreen: XCUIElement { app.element(UITestID.Onboarding.time) }
    var dayScreen: XCUIElement { app.element(UITestID.Onboarding.day) }
    var styleScreen: XCUIElement { app.element(UITestID.Onboarding.style) }
    var subscriptionScreen: XCUIElement { app.element(UITestID.Onboarding.subscription) }

    var dayTodayButton: XCUIElement { app.element(UITestID.Onboarding.dayToday) }
    var dayYesterdayButton: XCUIElement { app.element(UITestID.Onboarding.dayYesterday) }
    var subscribeButton: XCUIElement { app.element(UITestID.Onboarding.subscribe) }
    var skipButton: XCUIElement { app.element(UITestID.Onboarding.skip) }

    // MARK: - Actions

    /// Swipe left to advance to the next onboarding page.
    func swipeToNext() {
        app.swipeLeft()
    }

    /// Complete the full onboarding flow by swiping through all screens and tapping "Maybe Later".
    func completeOnboarding() {
        // Welcome -> swipe
        if welcomeScreen.waitForExistence(timeout: 5) {
            swipeToNext()
        }

        // Time -> swipe
        // Time screen doesn't have a unique identifier, just swipe
        swipeToNext()

        // Day -> select Today, then swipe
        if dayTodayButton.waitForExistence(timeout: 3) {
            dayTodayButton.tapWhenReady()
        }
        swipeToNext()

        // Style -> swipe
        swipeToNext()

        // Subscription -> tap "Maybe Later"
        if skipButton.waitForExistence(timeout: 5) {
            skipButton.tapWhenReady()
        }
    }

    // MARK: - Assertions

    func assertVisible(file: StaticString = #file, line: UInt = #line) {
        XCTAssertTrue(
            welcomeScreen.waitForExistence(timeout: 5),
            "Onboarding welcome screen should be visible",
            file: file, line: line
        )
    }

    func assertDismissed(file: StaticString = #file, line: UInt = #line) {
        // After onboarding, the tab bar should be visible
        let tabBar = app.tabBars.firstMatch
        XCTAssertTrue(
            tabBar.waitForExistence(timeout: 10),
            "Tab bar should be visible after onboarding completes",
            file: file, line: line
        )
    }
}
