//
//  Models.swift
//  MagicCounter
//
//  Created by Atridad Lahiji on 2025-12-06.
//

import Foundation

// A single player's state
struct PlayerState: Codable, Identifiable, Equatable {
    var id: Int
    var name: String
    var life: Int
    var poison: Int
    var commanderDamages: [Int: Int] // attackerId: damage
    var scooped: Bool
    
    var isEliminated: Bool {
        life <= 0 || scooped || commanderDamages.values.contains { $0 >= 21 } || poison >= 10
    }
}

// The full game state
struct GameState: Codable, Equatable {
    var players: [PlayerState]
    var startingLife: Int
    var trackPoison: Bool
    var trackCommanderDamage: Bool
    var stopped: Bool
    
    var winner: PlayerState? {
        let activePlayers = players.filter { !$0.isEliminated }
        if activePlayers.count == 1 {
            return activePlayers.first
        }
        return nil
    }
}

// A record of a match
struct MatchRecord: Codable, Identifiable, Equatable {
    var id: String
    var name: String
    var startedAt: Date
    var lastUpdated: Date
    var ongoing: Bool
    var winnerPlayerId: Int?
    var state: GameState
}
