import XCTest

/// Centralized app launch configuration for UI tests.
///
/// Provides consistent launch arguments and environment variables across
/// all test suites. Disables animations and sets locale to English for
/// deterministic test behavior.
enum TestLaunchConfig {

    /// Standard launch arguments for UI test mode.
    /// Includes `--ui-testing` so the app's `UITestRuntime.isEnabled` is true,
    /// which skips async auth checks, StoreKit, and push notification registration.
    static let standardArguments: [String] = [
        "--ui-testing",
        "--disable-animations",
        "--complete-onboarding",
        "-UITEST_MODE", "1",
        "-AppleLanguages", "(en)",
        "-AppleLocale", "en_US",
        "-UIAnimationsEnabled", "NO"
    ]

    /// Launch environment variables for UI tests.
    static let standardEnvironment: [String: String] = [
        "UITEST_MODE": "1",
        "ANIMATIONS_DISABLED": "1"
    ]

    /// Configure and launch app with standard test settings.
    ///
    /// - Parameters:
    ///   - additionalArguments: Extra launch arguments to append.
    ///   - additionalEnvironment: Extra environment variables to merge.
    /// - Returns: The launched `XCUIApplication` instance.
    @discardableResult
    static func launchApp(
        additionalArguments: [String] = [],
        additionalEnvironment: [String: String] = [:]
    ) -> XCUIApplication {
        let app = XCUIApplication()
        app.launchArguments = standardArguments + additionalArguments
        var env = standardEnvironment
        additionalEnvironment.forEach { env[$0.key] = $0.value }
        app.launchEnvironment = env
        app.launch()
        return app
    }

    /// Launch app pre-authenticated (skips login flow).
    ///
    /// Passes test credentials via launch arguments and environment so the
    /// app can bypass the normal authentication flow during UI tests.
    ///
    /// - Parameters:
    ///   - email: Test user email address.
    ///   - token: Test authentication token.
    /// - Returns: The launched `XCUIApplication` instance.
    @discardableResult
    static func launchAuthenticated(
        email: String = "test@example.com",
        token: String = "test-token-12345"
    ) -> XCUIApplication {
        return launchApp(
            additionalArguments: ["-TEST_AUTH_EMAIL", email, "-TEST_AUTH_TOKEN", token],
            additionalEnvironment: ["TEST_AUTH_EMAIL": email, "TEST_AUTH_TOKEN": token]
        )
    }
}
