import Foundation
import Testing
@testable import 康康

struct HealthProfileImportTests {
    @Test func previewKeepsExistingValuesWhenHealthKitValueIsMissing() {
        let existing = UserProfile(
            birthYear: 1988,
            biologicalSexRaw: "female",
            heightCM: 162,
            bloodTypeRaw: "A"
        )
        let draft = HealthProfileImportDraft(heightCM: 175)

        let preview = HealthProfileImportPreview(draft: draft, current: existing)

        #expect(preview.birthYear.current == "1988")
        #expect(preview.birthYear.imported == nil)
        #expect(preview.birthYear.willUpdate == false)
        #expect(preview.sex.imported == nil)
        #expect(preview.height.imported == "175cm")
        #expect(preview.height.willUpdate == true)
        #expect(preview.bloodType.imported == nil)
    }

    @Test func applyOnlyOverwritesFieldsPresentInDraft() {
        let profile = UserProfile(
            birthYear: 1988,
            biologicalSexRaw: "female",
            heightCM: 162,
            bloodTypeRaw: "A"
        )
        let draft = HealthProfileImportDraft(
            birthYear: 1990,
            biologicalSexRaw: "male",
            heightCM: nil,
            bloodTypeRaw: "O"
        )

        draft.apply(to: profile, now: Date(timeIntervalSince1970: 123))

        #expect(profile.birthYear == 1990)
        #expect(profile.biologicalSexRaw == "male")
        #expect(profile.heightCM == 162)
        #expect(profile.bloodTypeRaw == "O")
        #expect(profile.updatedAt == Date(timeIntervalSince1970: 123))
    }

    @Test func emptyDraftReportsNoImportableFields() {
        let draft = HealthProfileImportDraft()

        #expect(draft.hasAnyImportableField == false)
    }
}
