import SwiftUI

/// Shown before onboarding on a fresh install when iCloud is available.
/// Lets the user choose between restoring from iCloud or starting fresh.
struct OnboardingICloudCheckView: View {
    /// Called when the user decides to start fresh (no iCloud restore).
    let onSkip: () -> Void

    @AppStorage("cloudSyncEnabled") private var cloudSyncEnabled = false
    @State private var showRestartPrompt = false

    var body: some View {
        if showRestartPrompt {
            restartPromptView
        } else {
            checkView
        }
    }

    // MARK: - Check View

    private var checkView: some View {
        VStack {
            Spacer()

            VStack(spacing: 28) {
                ZStack {
                    Circle()
                        .fill(Color.appPrimary.opacity(0.12))
                        .frame(width: 140, height: 140)
                    Circle()
                        .fill(Color.appPrimary.opacity(0.22))
                        .frame(width: 100, height: 100)
                    Image(systemName: "icloud.fill")
                        .font(.system(size: 50))
                        .foregroundColor(.appPrimary)
                }

                VStack(spacing: 14) {
                    Text(String(localized: "icloud_check_title"))
                        .font(.title.weight(.bold))
                        .multilineTextAlignment(.center)

                    Text(String(localized: "icloud_check_description"))
                        .font(.body)
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.center)
                        .padding(.horizontal, 40)
                }
            }

            Spacer()
            Spacer()

            VStack(spacing: 12) {
                Button {
                    cloudSyncEnabled = true
                    showRestartPrompt = true
                } label: {
                    Label("Restore from iCloud", systemImage: "icloud.and.arrow.down")
                        .font(.headline)
                        .foregroundColor(.white)
                        .frame(maxWidth: .infinity)
                        .padding()
                        .background(Color.appPrimary)
                        .cornerRadius(AppConstants.UI.cornerRadius)
                }

                Button {
                    onSkip()
                } label: {
                    Text("Start Fresh")
                        .font(.subheadline)
                        .foregroundColor(.secondary)
                }
            }
            .padding(.horizontal, 24)
            .padding(.bottom, 40)
        }
        .background(AppBackground())
    }

    // MARK: - Restart Prompt View

    private var restartPromptView: some View {
        VStack {
            Spacer()

            VStack(spacing: 28) {
                ZStack {
                    Circle()
                        .fill(Color.positiveGreen.opacity(0.12))
                        .frame(width: 140, height: 140)
                    Circle()
                        .fill(Color.positiveGreen.opacity(0.22))
                        .frame(width: 100, height: 100)
                    Image(systemName: "checkmark.icloud.fill")
                        .font(.system(size: 50))
                        .foregroundColor(.positiveGreen)
                }

                VStack(spacing: 14) {
                    Text(String(localized: "icloud_enabled_title"))
                        .font(.title.weight(.bold))
                        .multilineTextAlignment(.center)

                    Text(String(localized: "icloud_enabled_description"))
                        .font(.body)
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.center)
                        .padding(.horizontal, 40)
                }
            }

            Spacer()
            Spacer()

            // "Got it" just acknowledges — the user must close and reopen manually.
            // The button stays active so it doesn't look broken.
            Button {
                // No-op: user needs to close and reopen the app.
                // Nothing to navigate to; this session has no CloudKit container.
            } label: {
                Text("Got it")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.positiveGreen)
                    .cornerRadius(AppConstants.UI.cornerRadius)
            }
            .padding(.horizontal, 24)
            .padding(.bottom, 40)
        }
        .background(AppBackground())
    }
}

#Preview {
    OnboardingICloudCheckView(onSkip: {})
}
