//
//  PhotoReviewView.swift
//  CameraTester
//
//  Created by Matt Bruce on 1/2/26.
//

import SwiftUI
import Bedrock

// MARK: - Photo Review View

struct PhotoReviewView: View {
    let photo: CapturedPhoto
    let isSaving: Bool
    let saveError: String?
    let onRetake: () -> Void
    let onSave: () -> Void

    var body: some View {
        ZStack {
            // Photo display
            Color.black
                .ignoresSafeArea()

            Image(uiImage: photo.image)
                .resizable()
                .scaledToFit()
                .ignoresSafeArea()

            // Top toolbar
            VStack {
                HStack {
                    // Retake button
                    Button(action: onRetake) {
                        Image(systemName: "arrow.triangle.2.circlepath")
                            .font(.system(size: 20, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 44, height: 44)
                            .background(
                                Circle()
                                    .fill(Color.black.opacity(0.6))
                            )
                    }

                    Spacer()

                    // Close button
                    Button(action: onRetake) {
                        Image(systemName: "xmark")
                            .font(.system(size: 20, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 44, height: 44)
                            .background(
                                Circle()
                                    .fill(Color.black.opacity(0.6))
                            )
                    }
                }
                .padding(.horizontal, Design.Spacing.large)
                .padding(.top, Design.Spacing.large)

                Spacer()

                // Bottom action bar
                VStack(spacing: Design.Spacing.medium) {
                    // Save status or error
                    if let error = saveError {
                        Text(error)
                            .foregroundColor(.red)
                            .font(.system(size: 14, weight: .medium))
                            .multilineTextAlignment(.center)
                            .padding(.vertical, Design.Spacing.small)
                            .padding(.horizontal, Design.Spacing.medium)
                            .background(
                                RoundedRectangle(cornerRadius: Design.CornerRadius.medium)
                                    .fill(Color.red.opacity(0.2))
                            )
                    } else if isSaving {
                        HStack(spacing: Design.Spacing.small) {
                            ProgressView()
                                .tint(.white)
                            Text("Saving...")
                                .foregroundColor(.white)
                                .font(.system(size: 16, weight: .medium))
                        }
                        .padding(.vertical, Design.Spacing.small)
                    }

                    // Action buttons
                    HStack(spacing: Design.Spacing.xLarge) {
                        // Share button
                        ShareButton(photo: photo.image)

                        // Save button
                        Button(action: onSave) {
                            ZStack {
                                Circle()
                                    .fill(Color.white)
                                    .frame(width: 80, height: 80)

                                Image(systemName: "checkmark")
                                    .font(.system(size: 24, weight: .bold))
                                    .foregroundColor(.black)
                            }
                            .shadow(radius: 5)
                        }
                        .disabled(isSaving)
                    }
                    .padding(.bottom, Design.Spacing.large)
                }
                .padding(.horizontal, Design.Spacing.large)
            }
        }
        .accessibilityLabel("Photo review")
        .accessibilityHint("Use the buttons at the bottom to save or share your photo")
    }
}
