import Foundation
import SwiftData

@Model final class TypeDelta {
    var id: UUID = UUID()
    var typeIdentifier: String = ""
    var displayName: String = ""
    var countDelta: Int = 0
    var hashBefore: String = ""
    var hashAfter: String = ""
    var qualityBeforeRaw: String?
    var qualityAfterRaw: String?
    var transitionRaw: String = "unchanged"
    var reasonRaw: String = "normal"
    var yearlyCountNote: String = ""
    var delta: SnapshotDelta?

    init(typeIdentifier: String, displayName: String) {
        self.id = UUID()
        self.typeIdentifier = typeIdentifier
        self.displayName = displayName
    }
}

extension TypeDelta {
    var transition: TypeTransition {
        get { TypeTransition(rawValue: transitionRaw) ?? .unchanged }
        set { transitionRaw = newValue.rawValue }
    }
    var reason: TypeDeltaReason {
        get { TypeDeltaReason(rawValue: reasonRaw) ?? .unknown }
        set { reasonRaw = newValue.rawValue }
    }
    var qualityBefore: SnapshotQuality? {
        get { qualityBeforeRaw.flatMap { SnapshotQuality(rawValue: $0) } }
        set { qualityBeforeRaw = newValue?.rawValue }
    }
    var qualityAfter: SnapshotQuality? {
        get { qualityAfterRaw.flatMap { SnapshotQuality(rawValue: $0) } }
        set { qualityAfterRaw = newValue?.rawValue }
    }
}
