import Foundation
import UIKit
import ComposeApp

/// Runtime contract between the app and XCUITests.
enum UITestRuntime {
    static let uiTestingFlag = "--ui-testing"
    static let disableAnimationsFlag = "--disable-animations"
    static let resetStateFlag = "--reset-state"
    static let mockAuthFlag = "--ui-test-mock-auth"
    static let completeOnboardingFlag = "--complete-onboarding"

    static var launchArguments: [String] {
        ProcessInfo.processInfo.arguments
    }

    static var isEnabled: Bool {
        launchArguments.contains(uiTestingFlag)
    }

    static var shouldDisableAnimations: Bool {
        isEnabled && launchArguments.contains(disableAnimationsFlag)
    }

    static var shouldResetState: Bool {
        isEnabled && launchArguments.contains(resetStateFlag)
    }

    static var shouldMockAuth: Bool {
        isEnabled && launchArguments.contains(mockAuthFlag)
    }

    static var shouldCompleteOnboarding: Bool {
        isEnabled && launchArguments.contains(completeOnboardingFlag)
    }

    static func configureForLaunch() {
        guard isEnabled else { return }

        if shouldDisableAnimations {
            UIView.setAnimationsEnabled(false)
        }

        UserDefaults.standard.set(true, forKey: "ui_testing_mode")

        // Mark onboarding complete synchronously before SwiftUI renders,
        // so RootView routes to the standalone LoginView instead of OnboardingCoordinator.
        if shouldCompleteOnboarding {
            UserDefaults.standard.set(true, forKey: "hasCompletedOnboarding")
        }
    }

    @MainActor static func resetStateIfRequested() {
        guard shouldResetState else { return }

        DataManager.shared.clear()
        OnboardingState.shared.reset()
        ThemeManager.shared.currentTheme = .bright
        UserDefaults.standard.removeObject(forKey: "ui_test_user_verified")

        // Re-apply onboarding completion after reset. Set the flag directly
        // because completeOnboarding() has an auth guard that fails here
        // (DataManager was just cleared, so isAuthenticated is false).
        if shouldCompleteOnboarding {
            OnboardingState.shared.hasCompletedOnboarding = true
        }
    }

    /// Mark onboarding as complete so the app shows the standalone login
    /// instead of the onboarding coordinator. Called after resetState (if any).
    @MainActor static func completeOnboardingIfRequested() {
        guard shouldCompleteOnboarding else { return }
        OnboardingState.shared.completeOnboarding()
    }
}
