//
//  BaccaratTests.swift
//  BaccaratTests
//
//  Created by Matt Bruce on 12/16/25.
//

import Testing
@testable import Baccarat
import CasinoKit

struct BaccaratTests {

    @Test func example() async throws {
        // Write your test here and use APIs like `#expect(...)` to check expected conditions.
    }
    
    @Test func handValueCalculation() async throws {
        var hand = Hand()
        
        // Test empty hand
        #expect(hand.value == 0)
        
        // Test with a King (value 0) and an 8 (value 8)
        hand.addCard(Card(suit: .spades, rank: .king))
        hand.addCard(Card(suit: .hearts, rank: .eight))
        #expect(hand.value == 8, "King (0) + Eight (8) should equal 8")
        
        // Test with two cards that sum to more than 10
        hand.clear()
        hand.addCard(Card(suit: .spades, rank: .seven))
        hand.addCard(Card(suit: .hearts, rank: .five))
        #expect(hand.value == 2, "Seven (7) + Five (5) = 12, 12 % 10 should equal 2")
        
        // Test natural 9
        hand.clear()
        hand.addCard(Card(suit: .clubs, rank: .four))
        hand.addCard(Card(suit: .diamonds, rank: .five))
        #expect(hand.value == 9, "Four (4) + Five (5) should equal 9")
        #expect(hand.isNatural == true, "Hand with 2 cards and value 9 is a natural")
        
        // Test with three cards
        hand.clear()
        hand.addCard(Card(suit: .spades, rank: .four))
        hand.addCard(Card(suit: .hearts, rank: .three))
        hand.addCard(Card(suit: .clubs, rank: .two))
        #expect(hand.value == 9, "Four (4) + Three (3) + Two (2) should equal 9")
        #expect(hand.isNatural == false, "Hand with 3 cards is not a natural")
        
        // Test face cards (all should be 0)
        hand.clear()
        hand.addCard(Card(suit: .spades, rank: .king))
        hand.addCard(Card(suit: .hearts, rank: .queen))
        #expect(hand.value == 0, "King (0) + Queen (0) should equal 0")
        
        // Test 10 card (should be 0 in baccarat)
        hand.clear()
        hand.addCard(Card(suit: .diamonds, rank: .ten))
        hand.addCard(Card(suit: .clubs, rank: .nine))
        #expect(hand.value == 9, "Ten (0) + Nine (9) should equal 9")
        
        // Test Ace (should be 1)
        hand.clear()
        hand.addCard(Card(suit: .hearts, rank: .ace))
        hand.addCard(Card(suit: .spades, rank: .ace))
        #expect(hand.value == 2, "Ace (1) + Ace (1) should equal 2")
        
        // Test modulo 10 with higher values
        hand.clear()
        hand.addCard(Card(suit: .diamonds, rank: .nine))
        hand.addCard(Card(suit: .clubs, rank: .nine))
        #expect(hand.value == 8, "Nine (9) + Nine (9) = 18, 18 % 10 should equal 8")
    }
    
    @Test func engineHandValues() async throws {
        var engine = BaccaratEngine(deckCount: 1)
        
        // Deal initial cards
        _ = engine.dealInitialCards()
        
        // Verify both hands have cards
        #expect(engine.playerHand.cards.count == 2)
        #expect(engine.bankerHand.cards.count == 2)
        
        // Verify hand values are calculated (between 0 and 9)
        #expect(engine.playerHand.value >= 0)
        #expect(engine.playerHand.value <= 9)
        #expect(engine.bankerHand.value >= 0)
        #expect(engine.bankerHand.value <= 9)
    }
    
    @Test @MainActor func gameStateHandValues() async throws {
        let state = GameState()
        
        // Initially should be 0
        #expect(state.playerHandValue == 0, "Player hand value should be 0 when no cards dealt")
        #expect(state.bankerHandValue == 0, "Banker hand value should be 0 when no cards dealt")
        
        // Place a bet
        state.placeBet(type: .player, amount: 100)
        
        // Deal cards
        await state.deal()
        
        // After dealing, both hands should have values between 0-9
        #expect(state.playerHandValue >= 0, "Player hand value should be >= 0")
        #expect(state.playerHandValue <= 9, "Player hand value should be <= 9")
        #expect(state.bankerHandValue >= 0, "Banker hand value should be >= 0")
        #expect(state.bankerHandValue <= 9, "Banker hand value should be <= 9")
        
        // Verify visible cards match engine cards
        #expect(state.visiblePlayerCards.count >= 2, "Player should have at least 2 cards")
        #expect(state.visibleBankerCards.count >= 2, "Banker should have at least 2 cards")
        
        // Verify cards are face up (needed for badges to show)
        #expect(state.playerCardsFaceUp.count >= 2, "Player should have face-up state for cards")
        #expect(state.bankerCardsFaceUp.count >= 2, "Banker should have face-up state for cards")
        #expect(state.playerCardsFaceUp.contains(true), "At least one player card should be face up")
        #expect(state.bankerCardsFaceUp.contains(true), "At least one banker card should be face up")
        
        // Verify the visible hand values match what the engine calculates
        let manualPlayerValue = state.visiblePlayerCards.reduce(0) { $0 + $1.baccaratValue } % 10
        let manualBankerValue = state.visibleBankerCards.reduce(0) { $0 + $1.baccaratValue } % 10
        #expect(state.playerHandValue == manualPlayerValue, "Visible cards should match calculated value for player")
        #expect(state.bankerHandValue == manualBankerValue, "Visible cards should match calculated value for banker")
    }

}
