//
//  BlackjackGameData.swift
//  Blackjack
//
//  Persistent game data model for iCloud sync.
//

import Foundation
import CasinoKit

/// Persistent game data that syncs to iCloud.
struct BlackjackGameData: PersistableGameData, SessionPersistable {
    static let gameIdentifier = "blackjack"
    
    var roundsPlayed: Int {
        // Total rounds from all sessions
        let historicalRounds = sessionHistory.reduce(0) { $0 + $1.roundsPlayed }
        let currentRounds = currentSession?.roundsPlayed ?? 0
        return historicalRounds + currentRounds
    }
    
    var lastModified: Date
    
    static var empty: BlackjackGameData {
        BlackjackGameData(
            lastModified: Date(),
            balance: 1_000,
            currentSession: nil,
            sessionHistory: []
        )
    }
    
    /// Current player balance.
    var balance: Int
    
    /// The currently active session (nil if no session started).
    var currentSession: BlackjackSession?
    
    /// History of completed sessions.
    var sessionHistory: [BlackjackSession]
}

/// Persistent settings data that syncs to iCloud.
struct BlackjackSettingsData: PersistableGameData {
    static let gameIdentifier = "blackjack_settings"
    var lastModified: Date
    var roundsPlayed: Int = 0  // Settings don't track rounds, use 0
    
    static var empty: BlackjackSettingsData {
        BlackjackSettingsData(
            lastModified: Date(),
            roundsPlayed: 0,
            gameStyle: "vegas",
            deckCount: 6,
            tableLimits: "casual",
            startingBalance: 1_000,
            dealerHitsSoft17: false,
            doubleAfterSplit: true,
            resplitAces: false,
            lateSurrender: false,
            noHoleCard: false,
            blackjackPayout: 1.5,
            insuranceAllowed: true,
            neverAskInsurance: false,
            showAnimations: true,
            dealingSpeed: CasinoDesign.DealingSpeed.normal,
            showCardsRemaining: true,
            showHistory: true,
            showHints: true,
            showCardCount: false,
            soundEnabled: true,
            hapticsEnabled: true,
            soundVolume: 1.0
        )
    }
    
    var gameStyle: String
    var deckCount: Int
    var tableLimits: String
    var startingBalance: Int
    var dealerHitsSoft17: Bool
    var doubleAfterSplit: Bool
    var resplitAces: Bool
    var lateSurrender: Bool
    var noHoleCard: Bool
    var blackjackPayout: Double
    var insuranceAllowed: Bool
    var neverAskInsurance: Bool
    var showAnimations: Bool
    var dealingSpeed: Double
    var showCardsRemaining: Bool
    var showHistory: Bool
    var showHints: Bool
    var showCardCount: Bool
    var soundEnabled: Bool
    var hapticsEnabled: Bool
    var soundVolume: Float
}
