import PhotosUI
import SwiftUI

struct PhotoOptionSheet: View {
    @Binding var selectedPhotos: [PhotosPickerItem]
    @Binding var imageData: [Data]
    let maxImages: Int
    let onCameraSelected: () -> Void
    let onPhotoLibrarySelected: () -> Void
    let onDismiss: () -> Void
    @EnvironmentObject var themeManager: ThemeManager

    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                Text("Add Photo")
                    .font(.title2)
                    .fontWeight(.semibold)
                    .padding(.top)

                Text("Choose how you'd like to add a photo")
                    .font(.subheadline)
                    .foregroundColor(.secondary)

                VStack(spacing: 16) {
                    Button(action: {
                        onPhotoLibrarySelected()
                        onDismiss()
                    }) {
                        HStack {
                            Image(systemName: "photo.on.rectangle")
                                .font(.title2)
                                .foregroundColor(themeManager.accentColor)
                            Text("Photo Library")
                                .font(.headline)
                            Spacer()
                            Image(systemName: "chevron.right")
                                .font(.caption)
                                .foregroundColor(.secondary)
                        }
                        .padding()
                        .background(.regularMaterial)
                        .cornerRadius(12)
                    }
                    .buttonStyle(PlainButtonStyle())

                    Button(action: {
                        onDismiss()
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                            onCameraSelected()
                        }
                    }) {
                        HStack {
                            Image(systemName: "camera.fill")
                                .font(.title2)
                                .foregroundColor(themeManager.accentColor)
                            Text("Camera")
                                .font(.headline)
                            Spacer()
                            Image(systemName: "chevron.right")
                                .font(.caption)
                                .foregroundColor(.secondary)
                        }
                        .padding()
                        .background(.regularMaterial)
                        .cornerRadius(12)
                    }
                    .buttonStyle(PlainButtonStyle())
                }
                .padding(.horizontal)

                Spacer()
            }
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Cancel") {
                        onDismiss()
                    }
                }
            }
        }
        .presentationDetents([.height(300)])
        .interactiveDismissDisabled(false)
    }
}
