//
//  HistoryDayCell.swift
//  Andromida
//
//  A single day cell in the history calendar showing completion progress.
//

import SwiftUI
import Bedrock

/// A single day cell showing the day number and a progress ring.
struct HistoryDayCell: View {
    let date: Date
    let progress: Double
    let isToday: Bool
    var isFuture: Bool = false
    let onTap: () -> Void
    
    private let calendar = Calendar.current
    
    private var dayNumber: Int {
        calendar.component(.day, from: date)
    }
    
    private var textColor: Color {
        if isFuture {
            return AppTextColors.tertiary
        } else if isToday {
            return AppAccent.primary
        } else {
            return AppTextColors.primary
        }
    }
    
    var body: some View {
        Button(action: onTap) {
            ZStack {
                // Progress ring (or empty ring for future days)
                ProgressRing(progress: progress, size: AppMetrics.Size.historyDayCell)
                
                // Day number
                Text("\(dayNumber)")
                    .font(.caption2)
                    .fontWeight(isToday ? .bold : .regular)
                    .foregroundStyle(textColor)
            }
            .frame(width: AppMetrics.Size.historyDayCell, height: AppMetrics.Size.historyDayCell)
        }
        .buttonStyle(.plain)
        .disabled(isFuture)
        .accessibilityElement(children: .combine)
        .accessibilityLabel(accessibilityLabel)
        .accessibilityHint(isFuture ? "" : String(localized: "Tap to view details"))
    }
    
    private var accessibilityLabel: String {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        let dateString = formatter.string(from: date)
        if isFuture {
            return "\(dateString), future date"
        }
        let percentComplete = Int(progress * 100)
        return "\(dateString), \(percentComplete) percent complete"
    }
}

/// An empty placeholder cell for days outside the current month.
struct HistoryDayPlaceholder: View {
    var body: some View {
        Color.clear
            .frame(width: AppMetrics.Size.historyDayCell, height: AppMetrics.Size.historyDayCell)
    }
}

#Preview {
    HStack(spacing: Design.Spacing.small) {
        HistoryDayCell(date: Date(), progress: 1.0, isToday: true) {}
        HistoryDayCell(date: Date(), progress: 0.66, isToday: false) {}
        HistoryDayCell(date: Date(), progress: 0.33, isToday: false) {}
        HistoryDayCell(date: Date(), progress: 0.0, isToday: false) {}
    }
    .padding()
    .background(AppSurface.primary)
}
