import Foundation
import SwiftData

@Model
final class TypeCount {
    var id: UUID = UUID()
    var typeIdentifier: String = ""
    var displayName: String = ""
    // count = -1 → query could not be completed (failed, timed out, unauthorized, unsupported, loading)
    // count = 0  → valid ONLY when quality == .complete; means HK returned no records
    var count: Int = 0
    var hash: String = ""               // SHA256(typeIdentifier|totalCount|earliestDate|latestDate)
    var earliestDate: Date? = nil
    var latestDate: Date? = nil
    var quality: SnapshotQuality = SnapshotQuality.complete
    // true when HKObjectType factory returned nil for this identifier (unsupported on this OS/device)
    var isUnsupported: Bool = false

    var snapshot: HealthSnapshot?

    @Relationship(deleteRule: .cascade, inverse: \YearlyCount.typeCount)
    var yearlyCounts: [YearlyCount]? = []

    init(typeIdentifier: String, displayName: String, count: Int, quality: SnapshotQuality = SnapshotQuality.complete) {
        self.id = UUID()
        self.typeIdentifier = typeIdentifier
        self.displayName = displayName
        self.count = count
        self.quality = quality
        self.yearlyCounts = []
    }
}
