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

import Foundation
import CasinoKit

/// Saved round result for history.
struct SavedRoundResult: Codable, Equatable {
    let date: Date
    let mainResult: String  // "blackjack", "win", "lose", "push", "bust", "surrender"
    let hadSplit: Bool
    let totalWinnings: Int
}

/// Persistent game data that syncs to iCloud.
struct BlackjackGameData: PersistableGameData {
    static let gameIdentifier = "blackjack"
    
    var roundsPlayed: Int { roundHistory.count }
    var lastModified: Date
    
    static var empty: BlackjackGameData {
        BlackjackGameData(
            lastModified: Date(),
            balance: 10_000,
            roundHistory: [],
            totalWinnings: 0,
            biggestWin: 0,
            biggestLoss: 0,
            blackjackCount: 0,
            bustCount: 0
        )
    }
    
    var balance: Int
    var roundHistory: [SavedRoundResult]
    var totalWinnings: Int
    var biggestWin: Int
    var biggestLoss: Int
    var blackjackCount: Int
    var bustCount: Int
}

/// 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: "low",
            startingBalance: 10_000,
            dealerHitsSoft17: false,
            doubleAfterSplit: true,
            resplitAces: false,
            lateSurrender: true,
            noHoleCard: false,
            blackjackPayout: 1.5,
            insuranceAllowed: true,
            neverAskInsurance: false,
            sideBetsEnabled: false,
            showAnimations: true,
            dealingSpeed: 1.0,
            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 sideBetsEnabled: 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
}

