import SwiftUI
import PhotosUI
import Bedrock

/// A reusable photo source picker that shows options before accessing the camera roll.
/// Supports: photo library, camera, and remove (if photo exists).
/// Can be extended with additional custom options per use case.
struct PhotoSourcePicker: View {
    @Environment(\.dismiss) private var dismiss
    
    let title: String
    let hasExistingPhoto: Bool
    let additionalOptions: [PhotoSourceOption]
    let onSelectFromLibrary: () -> Void
    let onTakePhoto: () -> Void
    let onRemovePhoto: (() -> Void)?
    let onOptionSelected: ((PhotoSourceOption) -> Void)?
    
    init(
        title: String = "Add photo",
        hasExistingPhoto: Bool = false,
        additionalOptions: [PhotoSourceOption] = [],
        onSelectFromLibrary: @escaping () -> Void,
        onTakePhoto: @escaping () -> Void,
        onRemovePhoto: (() -> Void)? = nil,
        onOptionSelected: ((PhotoSourceOption) -> Void)? = nil
    ) {
        self.title = title
        self.hasExistingPhoto = hasExistingPhoto
        self.additionalOptions = additionalOptions
        self.onSelectFromLibrary = onSelectFromLibrary
        self.onTakePhoto = onTakePhoto
        self.onRemovePhoto = onRemovePhoto
        self.onOptionSelected = onOptionSelected
    }
    
    private var optionCount: Int {
        var count = 2 // Library + Camera
        count += additionalOptions.count
        if hasExistingPhoto { count += 1 } // Remove option
        return count
    }
    
    var body: some View {
        NavigationStack {
            VStack(spacing: 0) {
                // Options list
                VStack(spacing: 0) {
                    // Select from photo library
                    PhotoSourcePickerOptionRow(
                        icon: "photo.on.rectangle",
                        title: String.localized("Select from photo library"),
                        action: {
                            onSelectFromLibrary()
                            dismiss()
                        }
                    )
                    
                    Divider()
                        .padding(.leading, Design.Spacing.xLarge + Design.CardSize.socialIconSize)
                    
                    // Take photo
                    PhotoSourcePickerOptionRow(
                        icon: "camera",
                        title: String.localized("Take photo"),
                        action: {
                            onTakePhoto()
                            dismiss()
                        }
                    )
                    
                    // Additional custom options
                    ForEach(additionalOptions) { option in
                        Divider()
                            .padding(.leading, Design.Spacing.xLarge + Design.CardSize.socialIconSize)
                        
                        PhotoSourcePickerOptionRow(
                            icon: option.icon,
                            title: option.title,
                            action: {
                                onOptionSelected?(option)
                                dismiss()
                            }
                        )
                    }
                    
                    // Remove photo (if exists)
                    if hasExistingPhoto, let onRemovePhoto {
                        Divider()
                            .padding(.leading, Design.Spacing.xLarge + Design.CardSize.socialIconSize)
                        
                        PhotoSourcePickerOptionRow(
                            icon: "trash",
                            title: String.localized("Remove photo"),
                            isDestructive: true,
                            action: {
                                onRemovePhoto()
                                dismiss()
                            }
                        )
                    }
                }
                .background(Color.AppBackground.card)
                .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
                .padding(.horizontal, Design.Spacing.large)
                .padding(.top, Design.Spacing.medium)
                
                Spacer()
            }
            .background(Color.AppBackground.secondary)
            .navigationTitle(title)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button {
                        dismiss()
                    } label: {
                        Image(systemName: "xmark")
                            .typography(.bodyEmphasis)
                            .foregroundStyle(Color.Text.primary)
                    }
                }
            }
        }
        .presentationDetents([.height(CGFloat(optionCount * 56 + 100))])
        .presentationDragIndicator(.visible)
    }
}

// MARK: - Preview

#Preview {
    Text("Tap to show picker")
        .sheet(isPresented: .constant(true)) {
            PhotoSourcePicker(
                title: "Add profile picture",
                hasExistingPhoto: true,
                additionalOptions: [],
                onSelectFromLibrary: { Design.debugLog("Library") },
                onTakePhoto: { Design.debugLog("Camera") },
                onRemovePhoto: { Design.debugLog("Remove") }
            )
        }
}
