//
//  RootView.swift
//  SelfieCam
//
//  Root view that manages the app's main navigation flow.
//  Shows onboarding on first launch, then the main camera view.
//

import SwiftUI
import Bedrock

struct RootView: View {
    /// Persistent flag for onboarding completion
    @AppStorage("hasCompletedOnboarding") private var hasCompletedOnboarding = false
    
    /// Settings view model shared between onboarding and main app
    @State private var settingsViewModel = SettingsViewModel()
    
    /// Onboarding view model
    @State private var onboardingViewModel = OnboardingViewModel()
    
    /// Whether to show the paywall (shared between views)
    @State private var showPaywall = false
    
    var body: some View {
        ZStack {
            if hasCompletedOnboarding {
                // Main app content
                ContentView()
                    .preferredColorScheme(.dark)
            } else {
                // Onboarding flow
                OnboardingContainerView(
                    viewModel: onboardingViewModel,
                    settingsViewModel: settingsViewModel,
                    showPaywall: $showPaywall,
                    onComplete: {
                        withAnimation(.easeInOut(duration: Design.Animation.standard)) {
                            hasCompletedOnboarding = true
                        }
                    }
                )
                .preferredColorScheme(.dark)
            }
        }
        .sheet(isPresented: $showPaywall) {
            PaywallPresenter {
                // Purchase successful during onboarding - complete it
                if !hasCompletedOnboarding {
                    onboardingViewModel.completeOnboarding(settingsViewModel: settingsViewModel)
                    withAnimation(.easeInOut(duration: Design.Animation.standard)) {
                        hasCompletedOnboarding = true
                    }
                }
            }
        }
    }
}

// MARK: - Preview

#Preview("Onboarding") {
    RootView()
}
