//
//  BlackjackStats.swift
//  Blackjack
//
//  Blackjack-specific statistics that conform to CasinoKit's GameSpecificStats.
//

import Foundation
import SwiftUI
import CasinoKit

/// Blackjack-specific session statistics.
/// Tracks blackjacks, busts, surrenders, doubles, and splits.
struct BlackjackStats: GameSpecificStats {
    /// Number of blackjacks hit.
    var blackjacks: Int = 0
    
    /// Number of busted hands.
    var busts: Int = 0
    
    /// Number of surrendered hands.
    var surrenders: Int = 0
    
    /// Number of doubled down hands.
    var doubles: Int = 0
    
    /// Number of split hands.
    var splits: Int = 0
    
    /// Number of insurance bets taken.
    var insuranceTaken: Int = 0
    
    /// Number of insurance bets won.
    var insuranceWon: Int = 0
    
    // MARK: - GameSpecificStats
    
    init() {}
    
    /// Display items for the statistics UI.
    var displayItems: [StatDisplayItem] {
        [
            StatDisplayItem(
                icon: "21.circle.fill",
                iconColor: .yellow,
                label: String(localized: "Blackjacks"),
                value: "\(blackjacks)",
                valueColor: .yellow
            ),
            StatDisplayItem(
                icon: "flame.fill",
                iconColor: .orange,
                label: String(localized: "Busts"),
                value: "\(busts)",
                valueColor: .orange
            ),
            StatDisplayItem(
                icon: "arrow.up.right.circle.fill",
                iconColor: .purple,
                label: String(localized: "Doubles"),
                value: "\(doubles)",
                valueColor: .purple
            ),
            StatDisplayItem(
                icon: "arrow.triangle.branch",
                iconColor: .blue,
                label: String(localized: "Splits"),
                value: "\(splits)",
                valueColor: .blue
            ),
            StatDisplayItem(
                icon: "flag.fill",
                iconColor: .gray,
                label: String(localized: "Surrenders"),
                value: "\(surrenders)",
                valueColor: .gray
            )
        ]
    }
}

// MARK: - Aggregation Extension

extension Array where Element == GameSession<BlackjackStats> {
    /// Aggregates Blackjack-specific stats from all sessions.
    func aggregatedBlackjackStats() -> BlackjackStats {
        var combined = BlackjackStats()
        
        for session in self {
            combined.blackjacks += session.gameStats.blackjacks
            combined.busts += session.gameStats.busts
            combined.surrenders += session.gameStats.surrenders
            combined.doubles += session.gameStats.doubles
            combined.splits += session.gameStats.splits
            combined.insuranceTaken += session.gameStats.insuranceTaken
            combined.insuranceWon += session.gameStats.insuranceWon
        }
        
        return combined
    }
}

// MARK: - Type Aliases for Convenience

/// Blackjack session type alias.
typealias BlackjackSession = GameSession<BlackjackStats>

