import Foundation
import SwiftData

/// Persisted state of an in-progress walk. Lives in SwiftData so a walk
/// survives an app crash or kill — on next launch the TrackingView offers
/// the user to resume, save, or discard.
@Model
final class ActiveWalk {
    var startedAt: Date
    var lastUpdate: Date
    var pausedAt: Date?
    var accumulatedPausedSeconds: Int
    private var pointsData: Data

    init(startedAt: Date = .now) {
        self.startedAt = startedAt
        self.lastUpdate = startedAt
        self.pausedAt = nil
        self.accumulatedPausedSeconds = 0
        self.pointsData = Data("[]".utf8)
    }

    var points: [GPSPoint] {
        get {
            (try? JSONDecoder().decode([GPSPoint].self, from: pointsData)) ?? []
        }
        set {
            pointsData = (try? JSONEncoder().encode(newValue)) ?? Data("[]".utf8)
        }
    }

    var isPaused: Bool { pausedAt != nil }
}
