import Foundation
import SwiftData

struct HealthRecordValue: Codable, Hashable, Identifiable, Sendable {
    var id: String { recordFingerprint }
    let typeIdentifier: String
    let sampleUUIDHash: String
    let recordFingerprint: String
    let startDate: Date
    let endDate: Date
    let displayValue: String?
}

enum HealthRecordArchive {
    static func encode(_ values: [HealthRecordValue]) -> Data? {
        let encoder = PropertyListEncoder()
        encoder.outputFormat = .binary
        return try? encoder.encode(values)
    }

    static func decode(_ data: Data) -> [HealthRecordValue]? {
        try? PropertyListDecoder().decode([HealthRecordValue].self, from: data)
    }
}

// Interface updated 2026-05-12 — see AGENTS.md
@Model final class HealthRecord {
    var id: UUID = UUID()
    var typeIdentifier: String = ""
    var sampleUUIDHash: String = ""
    var recordFingerprint: String = ""
    var startDate: Date = Date.distantPast
    var endDate: Date = Date.distantPast
    var displayValue: String?
    var typeCount: TypeCount?

    init(
        typeIdentifier: String,
        sampleUUIDHash: String,
        recordFingerprint: String,
        startDate: Date,
        endDate: Date,
        displayValue: String? = nil
    ) {
        self.id = UUID()
        self.typeIdentifier = typeIdentifier
        self.sampleUUIDHash = sampleUUIDHash
        self.recordFingerprint = recordFingerprint
        self.startDate = startDate
        self.endDate = endDate
        self.displayValue = displayValue
    }
}
