//
//  PhotoReviewView.swift
//  SelfieCam
//
//  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
    
    // Layout constants
    private let toolbarHeight: CGFloat = 100
    private let topButtonSize: CGFloat = 44      // Retake, Close (smaller, out of the way)
    private let bottomButtonSize: CGFloat = 64   // Share, Save (larger, main actions)

    var body: some View {
        ZStack {
            // Black background
            Color.black
                .ignoresSafeArea()

            // Photo - centered in available space
            Image(uiImage: photo.image)
                .resizable()
                .scaledToFit()
                .ignoresSafeArea()
        }
        // Top toolbar with gradient background
        .overlay(alignment: .top) {
            ZStack(alignment: .bottom) {
                // Gradient background for visibility
                LinearGradient(
                    colors: [Color.black.opacity(0.7), Color.black.opacity(0)],
                    startPoint: .top,
                    endPoint: .bottom
                )
                .frame(height: toolbarHeight)
                .ignoresSafeArea(edges: .top)
                
                // Buttons
                HStack {
                    // Retake button
                    Button(action: onRetake) {
                        Image(systemName: "arrow.triangle.2.circlepath")
                            .font(.system(size: 18, weight: .semibold))
                            .foregroundStyle(.white)
                            .frame(width: topButtonSize, height: topButtonSize)
                            .background(.ultraThinMaterial, in: Circle())
                    }
                    .accessibilityLabel("Retake photo")

                    Spacer()

                    // Close button
                    Button(action: onRetake) {
                        Image(systemName: "xmark")
                            .font(.system(size: 18, weight: .semibold))
                            .foregroundStyle(.white)
                            .frame(width: topButtonSize, height: topButtonSize)
                            .background(.ultraThinMaterial, in: Circle())
                    }
                    .accessibilityLabel("Close")
                }
                .padding(.horizontal, Design.Spacing.large)
                .padding(.bottom, Design.Spacing.medium)
            }
        }
        // Bottom toolbar with gradient background
        .overlay(alignment: .bottom) {
            ZStack(alignment: .top) {
                // Gradient background for visibility
                LinearGradient(
                    colors: [Color.black.opacity(0), Color.black.opacity(0.85)],
                    startPoint: .top,
                    endPoint: .bottom
                )
                .frame(height: 160)
                .ignoresSafeArea(edges: .bottom)
                
                VStack(spacing: Design.Spacing.medium) {
                    // Save status or error
                    if let error = saveError {
                        Text(error)
                            .foregroundStyle(.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...")
                                .foregroundStyle(.white)
                                .font(.system(size: 16, weight: .medium))
                        }
                    }

                    // Action buttons - same size, different styling
                    HStack(spacing: Design.Spacing.xLarge * 2) {
                        // Share button (frosted glass = secondary)
                        ShareButton(photo: photo.image)
                            .frame(width: bottomButtonSize, height: bottomButtonSize)
                            .background(.ultraThinMaterial, in: Circle())

                        // Save button (solid white = primary)
                        Button(action: onSave) {
                            Image(systemName: "checkmark")
                                .font(.system(size: 26, weight: .bold))
                                .foregroundStyle(.black)
                                .frame(width: bottomButtonSize, height: bottomButtonSize)
                                .background(.white, in: Circle())
                                .shadow(color: .black.opacity(0.3), radius: Design.Shadow.radiusMedium)
                        }
                        .disabled(isSaving)
                        .accessibilityLabel("Save photo")
                        .accessibilityHint("Saves the photo to your library")
                    }
                }
                .padding(.top, Design.Spacing.xLarge)
                .padding(.bottom, Design.Spacing.large)
            }
        }
        .accessibilityElement(children: .contain)
        .accessibilityLabel("Photo review")
    }
}
