//
//  FuelLogSummaryView.swift
//  Gas Man
//
//  Created by Kameron Kenny on 3/17/25.
//


import SwiftUI

private let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .short
    return formatter
}()

struct FuelLogSummaryView: View {
    var log: FuelLog
    var mpg: Double?
    var distanceSincePrevious: Double?
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            // Row 1: Date and MPG (if available)
            HStack {
                Text(log.date ?? Date(), formatter: dateFormatter)
                    .font(.subheadline)
                    .foregroundColor(.primary)
                Spacer()
                if let mpg = mpg {
                    Text("MPG: \(mpg, specifier: "%.3f")")
                        .font(.subheadline)
                        .foregroundColor(.secondary)
                }
            }
            Divider()
            // Row 2: Distance (instead of odometer) and Fuel Volume
            HStack {
                VStack(alignment: .leading) {
                    Text("Distance")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    if let distance = distanceSincePrevious {
                        Text("\(distance, specifier: "%.0f") miles")
                            .bold()
                    } else {
                        Text("N/A")
                            .bold()
                    }
                }
                Spacer()
                VStack(alignment: .leading) {
                    Text("Fuel")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text("\(log.fuelVolume, specifier: "%.3f") gal")
                        .bold()
                }
            }
            // Row 3: Cost and Price per Gallon
            HStack {
                VStack(alignment: .leading) {
                    Text("Cost")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text("$\(log.cost, specifier: "%.2f")")
                        .bold()
                }
                Spacer()
                VStack(alignment: .leading) {
                    Text("Price/Gal")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text("$\(log.pricePerGalon, specifier: "%.3f")")
                        .bold()
                }
            }
        }
        .padding(8)
        .background(Color(.secondarySystemBackground))
        .cornerRadius(8)
    }
}
