import SwiftUI

/// Root view for the App Clip that manages the card loading and display flow.
struct ClipRootView: View {
    @State private var store = ClipCardStore()
    let recordName: String

    var body: some View {
        ZStack {
            Color.Clip.background
                .ignoresSafeArea()

            Group {
                switch store.state {
                case .loading:
                    ClipLoadingView()
                case .loaded(let snapshot):
                    ClipCardPreview(snapshot: snapshot) {
                        Task { await store.saveToContacts() }
                    }
                case .saved:
                    ClipSuccessView()
                case .error(let message):
                    ClipErrorView(message: message) {
                        Task { await store.load(recordName: recordName) }
                    }
                }
            }
        }
        .task {
            await store.load(recordName: recordName)
        }
    }
}
