import Foundation
import SwiftData

@Model final class AnomalyRecord {
    @Attribute(.unique) var id: UUID
    @Attribute var detectedAt: Date
    @Attribute var snapshotID: UUID
    @Attribute var deltaID: UUID?
    @Attribute var deviceID: String
    @Attribute var anomalyTypeRaw: String
    @Attribute var severityRaw: String
    @Attribute var typeIdentifier: String?
    @Attribute var message: String
    @Attribute var isResolved: Bool

    init(snapshotID: UUID, deviceID: String, anomalyType: AnomalyType, severity: Severity) {
        self.id = UUID()
        self.detectedAt = Date.now
        self.snapshotID = snapshotID
        self.deltaID = nil
        self.deviceID = deviceID
        self.anomalyTypeRaw = anomalyType.rawValue
        self.severityRaw = severity.rawValue
        self.typeIdentifier = nil
        self.message = ""
        self.isResolved = false
    }
}

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 }
    }
}
