//
//  GameResult.swift
//  Baccarat
//
//  Game outcome types and result information.
//

import Foundation
import SwiftUI

/// The possible outcomes of a baccarat round.
enum GameResult: Equatable {
    case playerWins
    case bankerWins
    case tie
    
    /// Display text for the result (localized).
    var displayText: String {
        switch self {
        case .playerWins: return String.localized("PLAYER WINS")
        case .bankerWins: return String.localized("BANKER WINS")
        case .tie: return String.localized("TIE GAME")
        }
    }
    
    /// The color associated with this result.
    var color: Color {
        switch self {
        case .playerWins: return .blue
        case .bankerWins: return .red
        case .tie: return .green
        }
    }
    
    /// Whether the given bet type wins with this result.
    func isWinningBet(_ betType: BetType) -> Bool {
        switch (self, betType) {
        case (.playerWins, .player): return true
        case (.bankerWins, .banker): return true
        case (.tie, .tie): return true
        default: return false
        }
    }
    
    /// Returns true if this result is a push (tie) for non-tie bets.
    /// In baccarat, if the result is a tie, Player and Banker bets push (returned).
    func isPush(for betType: BetType) -> Bool {
        self == .tie && betType != .tie
    }
}

/// Record of a completed round for history tracking.
struct RoundResult: Identifiable {
    let id = UUID()
    let result: GameResult
    let playerValue: Int
    let bankerValue: Int
    let playerPair: Bool
    let bankerPair: Bool
    let timestamp: Date
    
    /// Whether either hand was a natural (8 or 9).
    var isNatural: Bool {
        playerValue >= 8 || bankerValue >= 8
    }
    
    /// Whether any pair occurred.
    var hasPair: Bool {
        playerPair || bankerPair
    }
    
    init(
        result: GameResult,
        playerValue: Int,
        bankerValue: Int,
        playerPair: Bool = false,
        bankerPair: Bool = false
    ) {
        self.result = result
        self.playerValue = playerValue
        self.bankerValue = bankerValue
        self.playerPair = playerPair
        self.bankerPair = bankerPair
        self.timestamp = .now
    }
}

