import Foundation
import SwiftData

@Model final class AnomalyRecord {
    var id: UUID = UUID()
    var detectedAt: Date = Date.now
    var snapshotID: UUID = UUID()
    var deltaID: UUID?
    var deviceID: String = ""
    var anomalyTypeRaw: String = "deletion"
    var severityRaw: String = "info"
    var typeIdentifier: String?
    var message: String = ""
    var isResolved: Bool = false

    init(snapshotID: UUID, deviceID: String, anomalyType: AnomalyType, severity: Severity) {
        self.id = UUID()
        self.snapshotID = snapshotID
        self.deviceID = deviceID
        self.anomalyTypeRaw = anomalyType.rawValue
        self.severityRaw = severity.rawValue
    }
}

extension AnomalyRecord {
    var anomalyType: AnomalyType {
        get { AnomalyType(rawValue: anomalyTypeRaw) ?? .deletion }
        set { anomalyTypeRaw = newValue.rawValue }
    }
    var severity: Severity {
        get { Severity(rawValue: severityRaw) ?? .info }
        set { severityRaw = newValue.rawValue }
    }
}
