import SwiftUI

private enum ArchiveStep: Hashable {
    case guide
    case scan
    case meta
    case progress
    case result
}

struct ArchiveFlow: View {
    var onClose: () -> Void

    @State private var step: ArchiveStep = .guide
    @State private var capturedPages: Int = 1
    @State private var totalPages: Int = 3

    var body: some View {
        ZStack {
            switch step {
            case .guide:
                B1GuideView(
                    onSingle: { withAnimation { totalPages = 1; step = .scan } },
                    onMulti:  { withAnimation { totalPages = 3; step = .scan } },
                    onSkip: onClose
                )
                .transition(.opacity)

            case .scan:
                B2ScanView(
                    onShoot: { capturedPages = min(capturedPages + 1, totalPages) },
                    onDone:  { withAnimation { step = .meta } },
                    onClose: onClose,
                    page: capturedPages,
                    total: totalPages
                )
                .transition(.opacity)

            case .meta:
                B3MetaView(
                    onAnalyze: { withAnimation { step = .progress } },
                    onBack:    { withAnimation { step = .scan } }
                )
                .transition(.opacity)

            case .progress:
                B4ProgressView(onComplete: {
                    withAnimation { step = .result }
                })
                .transition(.opacity)

            case .result:
                B5ResultView(
                    onSave: onClose,
                    onBack: { withAnimation { step = .meta } }
                )
                .transition(.opacity)
            }
        }
    }
}
