import Foundation
import SwiftData

@Model final class OperationLog {
    var id: UUID = UUID()
    var timestamp: Date = Date.now
    var operationType: String = ""
    var affectedSnapshotIDsJSON: String = "[]"
    var summary: String = ""
    var operationDeviceID: String = ""
    var operationAppBuildVersion: String = ""

    init(operationType: String, summary: String, deviceID: String, appBuildVersion: String) {
        self.id = UUID()
        self.operationType = operationType
        self.summary = summary
        self.operationDeviceID = deviceID
        self.operationAppBuildVersion = appBuildVersion
    }
}

extension OperationLog {
    var affectedSnapshotIDs: [String] {
        get { (try? JSONDecoder().decode([String].self, from: Data(affectedSnapshotIDsJSON.utf8))) ?? [] }
        set { affectedSnapshotIDsJSON = (try? String(data: JSONEncoder().encode(newValue), encoding: .utf8)) ?? "[]" }
    }
}
