import Foundation
import SwiftData

@Model
final class GameRecord {
    var variant: String
    var difficulty: String
    var snapshotData: Data
    var elapsedSeconds: Int
    var lastPlayedDate: Date

    init(variant: GameVariant, difficulty: Difficulty, snapshot: GameSnapshot, elapsedSeconds: Int) {
        self.variant = variant.rawValue
        self.difficulty = difficulty.rawValue
        self.snapshotData = (try? JSONEncoder().encode(snapshot)) ?? Data()
        self.elapsedSeconds = elapsedSeconds
        self.lastPlayedDate = Date()
    }

    var decodedSnapshot: GameSnapshot? {
        try? JSONDecoder().decode(GameSnapshot.self, from: snapshotData)
    }

    var gameVariant: GameVariant? {
        GameVariant(rawValue: variant)
    }

    var gameDifficulty: Difficulty? {
        Difficulty(rawValue: difficulty)
    }
}
