import SwiftUI
import PhotosUI
import Bedrock

/// A combined photo picker and cropper flow.
/// Shows the system PhotosPicker, and when an image is selected,
/// immediately overlays the cropper. Both dismiss together when done.
struct PhotoPickerWithCropper: View {
    @Environment(\.dismiss) private var dismiss
    
    let aspectRatio: CropAspectRatio
    let onSave: (Data) -> Void
    let onCancel: () -> Void
    
    init(
        aspectRatio: CropAspectRatio = .square,
        onSave: @escaping (Data) -> Void,
        onCancel: @escaping () -> Void
    ) {
        self.aspectRatio = aspectRatio
        self.onSave = onSave
        self.onCancel = onCancel
    }
    
    @State private var selectedItem: PhotosPickerItem?
    @State private var imageData: Data?
    @State private var showingCropper = false
    @State private var pickerRefreshID = UUID()  // Force picker refresh after cancel
    
    var body: some View {
        PhotosPicker(
            selection: $selectedItem,
            matching: .images,
            photoLibrary: .shared()
        ) {
            // This is never shown - we use it in inline mode
            EmptyView()
        }
        .photosPickerStyle(.inline)
        .photosPickerDisabledCapabilities([.selectionActions])
        .ignoresSafeArea()
        .id(pickerRefreshID)  // Force recreate picker when ID changes
        .onChange(of: selectedItem) { _, newValue in
            guard let newValue else { return }
            Task { @MainActor in
                if let data = try? await newValue.loadTransferable(type: Data.self) {
                    imageData = data
                    showingCropper = true
                }
            }
        }
        .overlay {
            // Cropper overlay
            if showingCropper, let imageData {
                PhotoCropperSheet(
                    imageData: imageData,
                    aspectRatio: aspectRatio,
                    shouldDismissOnComplete: false
                ) { croppedData in
                    if let croppedData {
                        onSave(croppedData)
                    } else {
                        // User cancelled cropper, go back to picker
                        showingCropper = false
                        self.imageData = nil
                        self.selectedItem = nil
                        // Generate new ID to force picker refresh, allowing same image to be reselected
                        pickerRefreshID = UUID()
                    }
                }
                .transition(.move(edge: .trailing))
                .id(aspectRatio.ratio)  // Force recreate cropper when aspect ratio changes
            }
        }
        .animation(.easeInOut(duration: Design.Animation.quick), value: showingCropper)
        .toolbar {
            // Only show cancel when cropper is NOT showing (cropper has its own toolbar)
            if !showingCropper {
                ToolbarItem(placement: .cancellationAction) {
                    Button(String.localized("Cancel")) {
                        onCancel()
                    }
                }
            }
        }
    }
}

#Preview {
    NavigationStack {
        PhotoPickerWithCropper(
            onSave: { _ in print("Saved") },
            onCancel: { print("Cancelled") }
        )
    }
}
