import Foundation

struct DataTypeSnapshotContext: Equatable, Sendable {
    let id: UUID
    let observedAt: Date
    let archiveObservationID: Int64?
}

@Observable
final class DataTypesViewModel {
    var filter: DiffFilter = .all
    var comparisonMode: ComparisonMode = .previous
    var archiveDiffs: [TypeDiff]?
    var archiveDiffError: String?

    func diffs() -> [TypeDiff] {
        guard let archiveDiffs else { return [] }
        return apply(filter: filter, to: archiveDiffs)
    }

    func baseline(for snapshot: DataTypeSnapshotContext, in snapshots: [DataTypeSnapshotContext]) -> DataTypeSnapshotContext? {
        resolveBaseline(for: snapshot, in: snapshots)
    }

    func clearArchiveDiffs() {
        archiveDiffs = nil
        archiveDiffError = nil
    }

    @MainActor
    func loadArchiveDiffs(current: DataTypeSnapshotContext?, snapshots: [DataTypeSnapshotContext]) async {
        guard let current,
              let baseline = resolveBaseline(for: current, in: snapshots),
              let currentObservationID = current.archiveObservationID,
              let baselineObservationID = baseline.archiveObservationID else {
            clearArchiveDiffs()
            return
        }

        do {
            let cache = try CoreDataArchiveCacheStore()
            let currentSummaries = try cache.typeSummaries(observationID: currentObservationID)
            let baselineSummaries = try cache.typeSummaries(observationID: baselineObservationID)
            let currentByType = Dictionary(uniqueKeysWithValues: currentSummaries.map { ($0.sampleTypeIdentifier, $0) })
            let baselineByType = Dictionary(uniqueKeysWithValues: baselineSummaries.map { ($0.sampleTypeIdentifier, $0) })
            let allTypeIdentifiers = Set(currentByType.keys).union(baselineByType.keys)

            var archiveRows: [TypeDiff] = []
            archiveRows.reserveCapacity(allTypeIdentifiers.count)

            for typeIdentifier in allTypeIdentifiers {
                let summary = currentByType[typeIdentifier]
                let baselineSummary = baselineByType[typeIdentifier]
                let diff = try await SQLiteHealthArchiveStore.shared.diffSummary(HealthArchiveDiffRequest(
                    fromObservationID: baselineObservationID,
                    toObservationID: currentObservationID,
                    sampleTypeIdentifier: typeIdentifier
                ))
                archiveRows.append(TypeDiff(
                    id: typeIdentifier,
                    typeIdentifier: typeIdentifier,
                    displayName: summary?.displayName ?? baselineSummary?.displayName ?? typeIdentifier,
                    currentCount: summary?.visibleRecordCount ?? 0,
                    previousCount: baselineSummary?.visibleRecordCount ?? 0,
                    previousTracked: baselineSummary != nil,
                    appearedCount: diff.appearedCount,
                    disappearedCount: diff.disappearedCount,
                    representationChangedCount: diff.representationChangedCount
                ))
            }

            archiveDiffs = archiveRows.sorted {
                $0.displayName.localizedCompare($1.displayName) == .orderedAscending
            }
            archiveDiffError = nil
        } catch {
            archiveDiffs = nil
            archiveDiffError = error.localizedDescription
        }
    }

    private func resolveBaseline(
        for snapshot: DataTypeSnapshotContext,
        in snapshots: [DataTypeSnapshotContext]
    ) -> DataTypeSnapshotContext? {
        let sorted = snapshots.sorted { $0.observedAt > $1.observedAt }
        switch comparisonMode {
        case .previous:
            return sorted.first { $0.observedAt < snapshot.observedAt }
        case .selected:
            return nil  // DataTypesView uses .previous by default; selection lives in SnapshotsTab
        case .relativeTime(let interval):
            let target = snapshot.observedAt.addingTimeInterval(-interval)
            return snapshots
                .filter { $0.observedAt <= target }
                .max { $0.observedAt < $1.observedAt }
        }
    }

    private func apply(filter: DiffFilter, to diffs: [TypeDiff]) -> [TypeDiff] {
        switch filter {
        case .all:       return diffs
        case .changed:   return diffs.filter { $0.previousTracked && $0.hasChanges }
        case .increased: return diffs.filter { $0.previousTracked && $0.delta > 0 }
        case .decreased: return diffs.filter { $0.previousTracked && $0.delta < 0 }
        }
    }
}
