import ComponentLibrary
import ComposableArchitecture
import FeatureMadlibsOnboarding
import FeatureOnboardingScreens
import FeatureSocial
import SwiftUI

// MARK: - OnboardingScreen

public struct OnboardingScreen: View {
    @Bindable var store: StoreOf<OnboardingReducer>

    public init(store: StoreOf<OnboardingReducer>) {
        self.store = store
    }

    public var body: some View {
        content
            .onAppear {
                store.send(.onAppear)
            }
    }

    private var content: some View {
        NavigationStack(
            path: $store.scope(state: \.path, action: \.path),
            root: {
                SignUpScreen(store: store.scope(state: \.signUp, action: \.signUp))
            },
            destination: { path in
                switch path.case {
                case .signUp(let signUpStore):
                    SignUpScreen(store: signUpStore)

                case .phoneSignIn(let phoneSignInStore):
                    PhoneSignInScreen(store: phoneSignInStore)

                case .phoneCode(let phoneCodeStore):
                    PhoneCodeEntryScreen(store: phoneCodeStore)

                case .displayName(let nameEntryStore):
                    NameEntryScreen(store: nameEntryStore)

                case .username(let usernameStore):
                    UsernameEntryScreen(store: usernameStore)

                case .birthday(let birthdayStore):
                    BirthdayEntryScreen(store: birthdayStore)

                case .notifications(let notificationsStore):
                    OnboardingNotificationsQuestionScreen(store: notificationsStore)

                case .contactsAuth(let contactsAuthStore):
                    ContactsAuthorizationScreen(store: contactsAuthStore)

                case .contactSync(let contactSyncStore):
                    ContactSyncScreen(store: contactSyncStore)
                        .navigationSwipeDisabled(navigationBarHidden: true)

                case .madlibsFlow:
                    if let madlibsContainerStore = store.scope(state: \.madlibsContainerState, action: \.madlibsContainer) {
                        MadlibsOnboardingContainerView(store: madlibsContainerStore)
                            .navigationSwipeDisabled(navigationBarHidden: true)
                    } else {
                        EmptyView()
                    }
                }
            }
        )
    }
}
