import SwiftUI
import SwiftData
import HealthKit
import UIKit

struct DashboardView: View {
    @Environment(\.modelContext) private var modelContext
    @Environment(AppSettings.self) private var appSettings
    @Query(sort: \HealthSnapshot.timestamp, order: .reverse) private var snapshots: [HealthSnapshot]
    @Query private var deviceProfiles: [DeviceProfile]
    @State private var viewModel = DashboardViewModel()
    @State private var didAutoRequestPermissions = false
    @State private var snapshotSheetTab: SnapshotSheetTab = .progress
    @State private var diagnosticReport: DiagnosticReport?
    @State private var expandedIssueIDs: Set<String> = []
    @State private var idleTimerWasDisabledBeforeSnapshot = false
    @State private var snapshotIdleTimerOverrideActive = false

    init() {
        let deviceID = AppSettings.currentDeviceID
        _snapshots = Query(
            filter: #Predicate<HealthSnapshot> { $0.deviceID == deviceID },
            sort: \HealthSnapshot.timestamp,
            order: .reverse
        )
    }

    private var latest: HealthSnapshot?   { snapshots.first }
    private var previous: HealthSnapshot? { snapshots.dropFirst().first }
    private var latestArchiveObservation: CachedArchiveObservationRow? {
        viewModel.latestArchiveObservation
    }
    private var previousArchiveObservation: CachedArchiveObservationRow? {
        guard viewModel.archiveObservationRows.count > 1 else { return nil }
        return viewModel.archiveObservationRows[1]
    }
    private var currentDeviceProfile: DeviceProfile? {
        deviceProfiles.first { $0.deviceID == AppSettings.currentDeviceID }
    }

    private var currentDeviceDisplayName: String {
        if let name = currentDeviceProfile?.name, !name.isEmpty { return name }
        if let latest, !latest.deviceName.isEmpty { return latest.deviceName }
        return "Local device"
    }

    private var latestUnavailableMetricCount: Int {
        guard let latest else { return 0 }
        return (latest.typeCounts ?? []).filter { $0.quality == .unauthorized }.count
    }

    private var latestArchiveChangeCount: Int? {
        guard let latestArchiveObservation,
              previousArchiveObservation != nil else {
            return nil
        }
        return latestArchiveObservation.appearedCount
            + latestArchiveObservation.disappearedCount
            + latestArchiveObservation.representationChangedCount
    }

    var body: some View {
        NavigationStack {
            List {
                statusSection
                anomalySummarySection
                actionsSection
                if let msg = viewModel.authError ?? viewModel.snapshotError {
                    Section {
                        Label(msg, systemImage: "exclamationmark.circle")
                            .foregroundStyle(Color.criticalRed)
                            .font(.caption)
                    }
                }
            }
            .navigationTitle("HealthProbe")
        }
        .sheet(isPresented: $viewModel.showProgressSheet) {
            progressSheet
        }
        .task {
            viewModel.loadArchiveCacheStatus()
            if !didAutoRequestPermissions && !HealthKitService.shared.hasRequestedPermissionsBefore {
                didAutoRequestPermissions = true
                await viewModel.requestAuthorization()
            }
        }
        .onChange(of: viewModel.snapshotProgress) { _, newProgress in
            updateIdleTimer(for: newProgress)
        }
        .onDisappear {
            restoreSnapshotIdleTimerIfNeeded()
        }
    }

    // MARK: - Helpers

    private func updateIdleTimer(for progress: SnapshotProgress) {
        let shouldDisableIdleTimer = progress == .fetching || progress == .processing

        if shouldDisableIdleTimer && !snapshotIdleTimerOverrideActive {
            idleTimerWasDisabledBeforeSnapshot = UIApplication.shared.isIdleTimerDisabled
            UIApplication.shared.isIdleTimerDisabled = true
            snapshotIdleTimerOverrideActive = true
        } else if !shouldDisableIdleTimer {
            restoreSnapshotIdleTimerIfNeeded()
        }
    }

    private func restoreSnapshotIdleTimerIfNeeded() {
        guard snapshotIdleTimerOverrideActive else { return }
        UIApplication.shared.isIdleTimerDisabled = idleTimerWasDisabledBeforeSnapshot
        snapshotIdleTimerOverrideActive = false
    }

    // MARK: - Report helpers

    private func failedTypesList(in progress: SnapshotFetchProgress) -> [SnapshotFetchProgress.TypeProgress] {
        progress.visibleTypes.filter {
            if case .failed = $0.status { return true }
            return false
        }
    }

    private func timedOutMetricNames(_ progress: SnapshotFetchProgress?) -> [String] {
        progress?.types.compactMap { type in
            if case .failed(let r) = type.status, r == "Timeout" { return type.displayName }
            return nil
        } ?? []
    }

    private func hasTimedOutMetrics(_ progress: SnapshotFetchProgress?) -> Bool {
        progress?.types.contains {
            if case .failed(let reason) = $0.status { return reason == "Timeout" }
            return false
        } ?? false
    }

    private func hasAuthorizationFailures(_ progress: SnapshotFetchProgress?) -> Bool {
        progress?.types.contains {
            if case .failed(let reason) = $0.status { return reason == "Not authorized" }
            return false
        } ?? false
    }

    private func degradedTypesList(in progress: SnapshotFetchProgress) -> [SnapshotFetchProgress.TypeProgress] {
        progress.types.filter { type in
            let hasAllExpectedCalls = Set(type.apiCallDetails.map(\.queryType)) == Set(["record_import", "earliest_sample", "latest_sample"])
            let allCallsComplete = type.apiCallDetails.allSatisfy { $0.status == .complete }
            return type.quality != "complete" || !hasAllExpectedCalls || !allCallsComplete
        }
    }

    private func formatDuration(_ seconds: TimeInterval) -> String {
        if seconds < 60 { return String(format: "%.1fs", seconds) }
        return "\(Int(seconds) / 60)m \(Int(seconds) % 60)s"
    }

    private func qualityLabel(progress: SnapshotFetchProgress) -> String {
        switch viewModel.snapshotProgress {
        case .complete: return "Complete"
        case .requiresResolution: return "Needs decision"
        case .incomplete:
            let hasUnauthorized = failedTypesList(in: progress).contains {
                if case .failed(let r) = $0.status { return r == "Not authorized" }
                return false
            }
            return hasUnauthorized ? "Partial (unauthorized)" : "Partial"
        default: return "—"
        }
    }

    private func qualityColor(progress: SnapshotFetchProgress) -> Color {
        switch viewModel.snapshotProgress {
        case .complete:   return .healthyGreen
        case .requiresResolution: return .warningAmber
        case .incomplete: return .warningAmber
        default:          return .neutralGray
        }
    }

    private func failureReason(_ type: SnapshotFetchProgress.TypeProgress) -> String {
        if case .failed(let r) = type.status { return r.isEmpty ? "Failed" : r }
        return "Unknown"
    }

    private func failureImpact(_ reason: String) -> String {
        switch reason {
        case "Not authorized": return "Excluded from checksum and change analysis"
        case "Timeout":        return "Data unavailable — type skipped this run"
        case "Unsupported":    return "Not supported on this device or OS version"
        default:               return "Data unavailable"
        }
    }

    private func remediationNoteItems(progress: SnapshotFetchProgress) -> [String] {
        let failed = failedTypesList(in: progress)
        var items: [String] = []
        let unauthorized = failed.filter {
            if case .failed(let r) = $0.status { return r == "Not authorized" }
            return false
        }
        let timedOut = failed.filter {
            if case .failed(let r) = $0.status { return r == "Timeout" }
            return false
        }
        if !unauthorized.isEmpty {
            items.append("Open iOS Settings, choose HealthProbe, and re-enable Health read access for the listed metrics.")
            items.append("After restoring access, create a new snapshot before relying on checksum or change analysis.")
        }
        if !timedOut.isEmpty {
            items.append("Timeout reflects HealthProbe's configured limit, not missing Health data or permission.")
        }
        if viewModel.snapshotProgress == .incomplete {
            items.append("Partial snapshots are excluded from change analysis. A complete snapshot is needed to resume integrity checks.")
        }
        return items
    }

    private func ambiguousDisappearanceNotes() -> [String] {
        [
            "All values are missing for the listed metric in the current Health view. Apple API does not allow HealthProbe to verify Health read authorization status.",
            "Possible causes: Health read authorization was withdrawn, or the metric is genuinely absent from the current Health store."
        ]
    }

    private func ambiguousDisappearanceActions() -> [String] {
        [
            "Treat the metric as unauthorized.",
            "Treat the metric as absent from the current Health store.",
            "Cancel saving this snapshot."
        ]
    }

    private func iso8601String(_ date: Date?) -> String {
        guard let date else { return "none" }
        return ISO8601DateFormatter().string(from: date)
    }

    private enum DiagnosticReportMode {
        case compact
        case full
    }

    private func operationResultValue() -> String {
        switch viewModel.snapshotProgress {
        case .complete:
            return "complete_success"
        case .incomplete:
            return "partial_success"
        case .requiresResolution:
            return "requires_user_resolution"
        case .idle, .fetching, .processing:
            return "failed"
        }
    }

    private func normalizedAuthorization(for type: SnapshotFetchProgress.TypeProgress) -> (status: String, source: String) {
        let hasSuccessfulQuery = type.apiCallDetails.contains { $0.status == .complete }
        let normalized = type.authorizationStatus.lowercased()

        if hasSuccessfulQuery {
            return ("granted", "inferredFromSuccessfulQuery")
        }

        if normalized == "denied" {
            return ("denied", "inferredFromUnauthorizedQuery")
        }

        if normalized == "notdetermined" {
            return ("notDetermined", "reported")
        }

        if normalized == "granted" {
            return ("granted", "reported")
        }

        // When real HK authorization status is unavailable, report unknown+unavailable.
        return ("unknown", "unavailable")
    }

    private func metricDateString(_ date: Date?, queryType: String, calls: [HealthKitAPICallResult]) -> String {
        if let date { return iso8601String(date) }
        guard let call = calls.first(where: { $0.queryType == queryType }) else { return "unknown" }
        return call.status == .complete ? "none" : "unknown"
    }

    private func yearlyCountsString(_ counts: [SnapshotFetchProgress.YearlyCountProgress]) -> String {
        guard !counts.isEmpty else { return "none" }
        return counts
            .sorted { $0.year < $1.year }
            .map { "\($0.year)=\($0.count)\($0.isApproximate ? " approximate" : "")" }
            .joined(separator: ", ")
    }

    private func callResultLabel(_ call: HealthKitAPICallResult) -> String {
        if let result = call.resultValue, !result.isEmpty {
            return result
        }
        return call.status == .complete ? "none" : "unknown"
    }

    private func shouldPrintCallErrorFields(_ call: HealthKitAPICallResult) -> Bool {
        switch call.status {
        case .failed, .timeout, .unknown, .unauthorized:
            return true
        case .complete, .unsupported:
            return false
        }
    }

    private func buildDiagnosticText(_ progress: SnapshotFetchProgress, mode: DiagnosticReportMode = .full) -> String {
        let snapshotID = viewModel.completedSnapshotID.map { $0.uuidString } ?? "unknown"
        let deviceID   = viewModel.completedSnapshotDeviceID ?? "unknown"
        let timestamp  = viewModel.completedSnapshotTimestamp ?? Date()
        let operationID = viewModel.operationID?.uuidString ?? "unknown"
        let reportGeneratedAt = iso8601String(Date())
        let trigger = viewModel.completedSnapshotTriggerReason ?? "manual"
        let quality: String
        switch viewModel.snapshotProgress {
        case .complete:   quality = "complete"
        case .incomplete: quality = "partial"
        case .requiresResolution: quality = "requires_resolution"
        default:          quality = "unknown"
        }
        let duration = viewModel.fetchDurationSeconds.map { formatDuration($0) } ?? "unknown"
        let fmt = DateFormatter()
        fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
        fmt.locale = Locale(identifier: "en_US_POSIX")
        let tsStr = fmt.string(from: timestamp)
        let degraded = degradedTypesList(in: progress)
        let isPartialSnapshot = viewModel.snapshotProgress == .incomplete
        let requiresResolution = viewModel.snapshotProgress == .requiresResolution
        let failedLines: String
        if degraded.isEmpty {
            failedLines = "FAILED/DEGRADED METRICS: none"
        } else {
            failedLines = (["FAILED/DEGRADED METRICS"] + degraded
                .map { "  \($0.displayName): \($0.quality)" })
                .joined(separator: "\n")
        }

        var lines: [String] = []
        lines.append("BEGIN HEALTHPROBE REPORT")
        lines.append("")
        lines.append("OPERATION METADATA")
        lines.append("operationType: \(trigger == "retryFailedMetrics" ? "retryFailedMetrics" : "createSnapshot")")
        lines.append("operationID: \(operationID)")
        lines.append("operationResult: \(operationResultValue())")
        lines.append("primaryObjectType: snapshot")
        lines.append("primaryObjectID: \(snapshotID)")
        lines.append("reportSchemaVersion: 1")
        lines.append("reportGeneratedAt: \(reportGeneratedAt)")
        lines.append("")
        lines.append("OPERATION SUMMARY")
        lines.append("Snapshot:   \(snapshotID)")
        lines.append("Device:     \(deviceID)")
        lines.append("Timestamp:  \(tsStr)")
        lines.append("Quality:    \(quality)")
        lines.append("Duration:   \(duration)")
        lines.append("Trigger:    \(trigger)")
        if let retryOfSnapshotID = viewModel.completedSnapshotRetryOfSnapshotID {
            lines.append("Retry of:   \(retryOfSnapshotID.uuidString)")
        }
        lines.append("")
        lines.append("INTERPRETATION_HINTS")
        lines.append("Partial snapshot: \(isPartialSnapshot ? "true" : "false")")
        lines.append("Requires user resolution: \(requiresResolution ? "true" : "false")")
        lines.append("Failed metrics are excluded from checksum/change analysis")
        lines.append("Do not infer deletion from partial snapshots")
        lines.append("All-values-missing metrics require review before saving")
        lines.append("Timeout means HealthProbe cancelled after configured timeout, not necessarily HealthKit denial")
        lines.append("")
        lines.append("CONFIGURATION")
        lines.append("adaptiveTimeoutsEnabled: \(progress.adaptiveTimeoutsEnabled ? "true" : "false")")
        lines.append("defaultInitialTimeout:   \(formatDuration(LocalMetricTimeoutProfile.defaultInitialTimeout))")
        lines.append("maximumTimeout:          \(formatDuration(LocalMetricTimeoutProfile.maximumTimeout))")
        lines.append("maxConcurrentTypeFetches: \(progress.maxConcurrentTypeFetches)")
        lines.append("")
        lines.append("CHAIN/INTEGRITY CONTEXT")
        lines.append("previousSnapshotID:      \(progress.previousSnapshotID.map { $0.uuidString } ?? "none")")
        lines.append("isChainStart:            \(progress.isChainStart.map { $0 ? "true" : "false" } ?? "unknown")")
        lines.append("snapshotChecksum:        \(progress.snapshotChecksum)")
        lines.append("monitoredTypeSetHash:    \(progress.monitoredTypeSetHash)")
        lines.append("monitoredRegistryVersion: \(progress.monitoredRegistryVersion.map(String.init) ?? "unknown")")
        lines.append("")
        lines.append("STATISTICS")
        lines.append("Records:    \(progress.totalRecords)")
        lines.append("Types:      \(progress.types.count)/\(progress.totalTypeCount) processed, \(progress.completedCount) complete, \(degraded.count) degraded")
        lines.append("")
        lines.append(failedLines)

        if requiresResolution {
            lines.append("")
            lines.append("AMBIGUOUS TOTAL DISAPPEARANCE")
            for metric in viewModel.ambiguousDisappearedMetrics {
                lines.append("  \(metric.displayName): previousCount=\(metric.previousCount), currentCount=0")
            }
            lines.append("  note: Apple API does not expose read authorization status for this decision point")
        }

        lines.append("")
        lines.append("HEALTHKIT API RESULTS")
        let orderedTypes = progress.types.sorted(by: { $0.displayName < $1.displayName })
        let degradedIDs = Set(degraded.map(\.id))
        let reportTypes: [SnapshotFetchProgress.TypeProgress]
        switch mode {
        case .compact:
            reportTypes = orderedTypes.filter { degradedIDs.contains($0.id) }
        case .full:
            reportTypes = orderedTypes
        }

        for type in reportTypes {
            lines.append("")
            lines.append("\(type.displayName):")
            lines.append("  identifier: \(type.id)")
            lines.append("  quality: \(type.quality)")
            lines.append("  count: \(type.recordCount)")
            lines.append("  timeoutMode: \(type.timeoutMode)")
            lines.append("  timeoutConfigured: \(formatDuration(type.timeoutConfiguredSeconds))")
            lines.append("  lastSuccessfulElapsed: \(type.lastSuccessfulElapsed > 0 ? formatDuration(type.lastSuccessfulElapsed) : "none")")
            lines.append("  learnedTimeout: \(type.learnedTimeout > 0 ? formatDuration(type.learnedTimeout) : "none")")
            lines.append("  suggestedRetryTimeout: \(type.suggestedRetryTimeout > 0 ? formatDuration(type.suggestedRetryTimeout) : "none")")
            lines.append("  timeoutCount: \(type.timeoutCount)")
            lines.append("  successCount: \(type.successCount)")
            lines.append("  totalElapsed: \(formatDuration(type.totalElapsedSeconds))")
            if type.blockElapsedSeconds > 0 {
                lines.append("  fetchProgressElapsed: \(formatDuration(type.blockElapsedSeconds))")
            }
            if type.blockSamplesPerSecond > 0 {
                lines.append("  fetchProgressRate: \(formatRate(type.blockSamplesPerSecond)) samples/s")
            }
            if mode == .full || type.isUnsupported {
                lines.append("  unsupported: \(type.isUnsupported ? "true" : "false")")
            }
            let auth = normalizedAuthorization(for: type)
            lines.append("  authorizationStatus: \(auth.status)")
            lines.append("  authorizationStatusSource: \(auth.source)")
            lines.append("  earliestDate: \(metricDateString(type.earliestDate, queryType: "earliest_sample", calls: type.apiCallDetails))")
            lines.append("  latestDate: \(metricDateString(type.latestDate, queryType: "latest_sample", calls: type.apiCallDetails))")
            lines.append("  yearlyCounts: \(yearlyCountsString(type.yearlyCounts))")
            if type.recordCount == 0 {
                let zeroReason = type.quality == "complete" ? "complete query with zero records" : "\(type.authorizationStatus) authorization/query state"
                lines.append("  zeroCountContext: \(zeroReason)")
            }
            if type.apiCallDetails.contains(where: { $0.status == .timeout }) {
                lines.append("  interpretation: timeout policy failure, not HealthKit denial")
                lines.append("  action: Retry with extended timeout")
            }

            lines.append("  apiCalls:")
            for queryType in ["record_import", "earliest_sample", "latest_sample"] {
                if let call = type.apiCallDetails.first(where: { $0.queryType == queryType }) {
                    lines.append("    \(queryType):")
                    lines.append("      status: \(call.statusDescription)")
                    lines.append("      elapsed: \(String(format: "%.2f", call.elapsedSeconds))s")
                    if mode == .full || call.status != .complete {
                        lines.append("      result: \(callResultLabel(call))")
                    }
                    if shouldPrintCallErrorFields(call) {
                        if let failureKind = call.failureKind, !failureKind.isEmpty, failureKind != "none" {
                            lines.append("      failureKind: \(failureKind)")
                        }
                        if let cancellationReason = call.cancellationReason, !cancellationReason.isEmpty, cancellationReason != "none" {
                            lines.append("      cancellationReason: \(cancellationReason)")
                        }
                        if let errorDomain = call.errorDomain, !errorDomain.isEmpty, errorDomain != "none" {
                            lines.append("      errorDomain: \(errorDomain)")
                        }
                        if let errorCode = call.errorCode, !errorCode.isEmpty, errorCode != "none" {
                            lines.append("      errorCode: \(errorCode)")
                        }
                        if let errorMessage = call.errorDescription, !errorMessage.isEmpty, errorMessage != "none" {
                            lines.append("      errorMessage: \(errorMessage)")
                        }
                    }
                } else {
                    lines.append("    \(queryType):")
                    lines.append("      status: unknown")
                    lines.append("      elapsed: 0.00s")
                    if mode == .full {
                        lines.append("      result: unknown")
                        lines.append("      failureKind: not_run")
                    }
                }
            }
        }

        lines.append("")
        lines.append("DEVICE/APP CONTEXT")
        lines.append("OS: \(UIDevice.current.systemVersion)")
        lines.append("App Version: \(Bundle.main.appVersion)")
        lines.append("")
        lines.append("END HEALTHPROBE REPORT")

        return lines.joined(separator: "\n")
    }

    private func plainTextStatus(for status: SnapshotFetchProgress.TypeProgress.FetchStatus) -> String {
        switch status {
        case .pending: return "Pending"
        case .fetching: return "Fetching"
        case .complete: return "Complete"
        case .failed(let message): return message.isEmpty ? "Failed" : "Failed (\(message))"
        }
    }

    private func fetchProgressSummary(_ progress: SnapshotFetchProgress) -> String {
        if progress.failedCount > 0 {
            return "\(progress.completedCount)/\(progress.totalTypeCount) fetched - \(progress.failedCount) failed"
        }
        return "\(progress.completedCount)/\(progress.totalTypeCount) fetched"
    }

    private func colorForStatus(_ status: SnapshotFetchProgress.TypeProgress.FetchStatus) -> Color {
        switch status {
        case .pending: return .gray
        case .fetching: return .blue
        case .complete: return .healthyGreen
        case .failed(let reason):
            return reason == "Not authorized" ? .warningAmber : .criticalRed
        }
    }

    private func rowBackground(for status: SnapshotFetchProgress.TypeProgress.FetchStatus) -> Color {
        switch status {
        case .fetching:
            return .blue.opacity(0.1)
        default:
            return Color(.systemGray6)
        }
    }

    private func rowBorder(for status: SnapshotFetchProgress.TypeProgress.FetchStatus) -> Color {
        switch status {
        case .fetching:
            return .blue.opacity(0.3)
        default:
            return .clear
        }
    }

    private var shouldShowFetchProgress: Bool {
        switch viewModel.snapshotProgress {
        case .fetching, .complete, .incomplete, .requiresResolution:
            return viewModel.fetchProgress != nil
        case .idle, .processing:
            return false
        }
    }

    private var shouldShowFetchReport: Bool {
        switch viewModel.snapshotProgress {
        case .complete, .incomplete, .requiresResolution:
            return true
        case .idle, .fetching, .processing:
            return false
        }
    }

    private func fetchReportView(_ progress: SnapshotFetchProgress) -> some View {
        let isIncomplete = viewModel.snapshotProgress == .incomplete
        let requiresResolution = viewModel.snapshotProgress == .requiresResolution
        let failed = isIncomplete ? failedTypesList(in: progress) : []
        let noteItems  = isIncomplete ? remediationNoteItems(progress: progress) : []
        let remediationTitle = hasAuthorizationFailures(progress) ? "WHAT TO DO" : "NOTES"
        return VStack(alignment: .leading, spacing: 20) {
            reportSummarySection(progress)
            if !failed.isEmpty { reportIssuesSection(failed) }
            if requiresResolution {
                ambiguousDisappearanceSection()
                reportRemediationSection(ambiguousDisappearanceNotes(), title: "NOTES")
                reportRemediationSection(ambiguousDisappearanceActions(), title: "WHAT TO DO")
            }
            if !noteItems.isEmpty { reportRemediationSection(noteItems, title: remediationTitle) }
            if isIncomplete, hasTimedOutMetrics(progress) { reportDecisionOverviewSection() }
            if isIncomplete { snapshotResultActionsSection }
            if requiresResolution { ambiguousResolutionActionsSection }
        }
    }

    private func reportSummarySection(_ progress: SnapshotFetchProgress) -> some View {
        VStack(alignment: .leading, spacing: 6) {
            Text("SUMMARY")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)

            let isComplete = viewModel.snapshotProgress == .complete
            let requiresResolution = viewModel.snapshotProgress == .requiresResolution
            HStack(spacing: 10) {
                Image(systemName: isComplete ? "checkmark.circle.fill" : "exclamationmark.triangle.fill")
                    .font(.title3)
                    .foregroundStyle(isComplete ? Color.healthyGreen : Color.warningAmber)
                VStack(alignment: .leading, spacing: 2) {
                    Text(isComplete ? "Snapshot created successfully" : (requiresResolution ? "Snapshot needs review" : "Partial snapshot"))
                        .font(.subheadline.weight(.semibold))
                    Text(isComplete ? "All monitored metrics loaded" : (requiresResolution ? "\(viewModel.ambiguousDisappearedMetrics.count) metric\(viewModel.ambiguousDisappearedMetrics.count == 1 ? "" : "s") need review" : "\(progress.failedCount) metric\(progress.failedCount == 1 ? "" : "s") failed"))
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }
                Spacer()
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 10)
            .background(isComplete ? Color.healthyGreen.opacity(0.08) : Color.warningAmber.opacity(0.08))
            .cornerRadius(8)

            VStack(spacing: 0) {
                ReportRow(label: "Types processed", value: "\(progress.types.count)/\(progress.totalTypeCount)")
                Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                ReportRow(label: "Successful", value: "\(progress.completedCount)", valueColor: .healthyGreen)
                if progress.failedCount > 0 {
                    Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                    ReportRow(label: "Failed", value: "\(progress.failedCount)", valueColor: .criticalRed)
                }
                Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                ReportRow(label: "Records fetched", value: "\(progress.totalRecords)")
                Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                ReportRow(
                    label: "Quality",
                    value: qualityLabel(progress: progress),
                    valueColor: qualityColor(progress: progress)
                )
                if let secs = viewModel.fetchDurationSeconds {
                    Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                    ReportRow(label: "Duration", value: formatDuration(secs))
                }
                Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                ReportRow(label: "Trigger", value: viewModel.completedSnapshotTriggerReason ?? "manual")
                Rectangle().fill(Color(.separator)).frame(height: 0.5).padding(.leading, 16)
                ReportActionRow(label: "Diagnostics", value: "View", systemImage: "doc.text.magnifyingglass") {
                    diagnosticReport = DiagnosticReport(text: buildDiagnosticText(progress, mode: .full))
                }
            }
            .background(Color(.systemGray6))
            .cornerRadius(10)
        }
    }

    private func ambiguousDisappearanceSection() -> some View {
        VStack(alignment: .leading, spacing: 6) {
            Text("ISSUES")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)

            Text("\(viewModel.ambiguousDisappearedMetrics.count) metric\(viewModel.ambiguousDisappearedMetrics.count == 1 ? "" : "s") returned zero samples")
                .font(.subheadline.weight(.semibold))
                .foregroundStyle(Color.warningAmber)

            VStack(spacing: 4) {
                ForEach(viewModel.ambiguousDisappearedMetrics) { metric in
                    HStack(spacing: 8) {
                        Image(systemName: "questionmark.circle.fill")
                            .foregroundStyle(Color.warningAmber)
                            .font(.caption)
                            .frame(width: 14)
                        Text(metric.displayName)
                            .font(.subheadline.weight(.semibold))
                        Spacer()
                        Text("all values missing")
                            .font(.caption)
                            .foregroundStyle(.secondary)
                    }
                    .padding(.horizontal, 12)
                    .padding(.vertical, 10)
                    .background(Color(.systemGray6))
                    .cornerRadius(8)
                }
            }
        }
    }

    private func reportIssuesSection(_ failed: [SnapshotFetchProgress.TypeProgress]) -> some View {
        VStack(alignment: .leading, spacing: 6) {
            Text("ISSUES")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)

            Text("\(failed.count) metric\(failed.count == 1 ? "" : "s") failed")
                .font(.subheadline.weight(.semibold))
                .foregroundStyle(Color.criticalRed)

            VStack(spacing: 4) {
                ForEach(failed) { type in
                    let reason = failureReason(type)
                    let isExpanded = expandedIssueIDs.contains(type.id)
                    VStack(alignment: .leading, spacing: 0) {
                        // Collapsed row (always visible)
                        Button {
                            withAnimation(.easeInOut(duration: 0.2)) {
                                if isExpanded {
                                    expandedIssueIDs.remove(type.id)
                                } else {
                                    expandedIssueIDs.insert(type.id)
                                }
                            }
                        } label: {
                            HStack(spacing: 8) {
                                Image(systemName: reason == "Not authorized"
                                      ? "exclamationmark.triangle.fill" : "xmark.circle.fill")
                                    .foregroundStyle(reason == "Not authorized"
                                                     ? Color.warningAmber : Color.criticalRed)
                                    .font(.caption)
                                    .frame(width: 14)
                                Text(type.displayName)
                                    .font(.subheadline.weight(.semibold))
                                Text("— \(reason)")
                                    .font(.subheadline)
                                    .foregroundStyle(.secondary)
                                Spacer()
                                Image(systemName: isExpanded ? "chevron.up" : "chevron.down")
                                    .font(.caption2.weight(.semibold))
                                    .foregroundStyle(.tertiary)
                            }
                            .padding(.horizontal, 12)
                            .padding(.vertical, 10)
                            .contentShape(Rectangle())
                        }
                        .buttonStyle(.plain)

                        // Expanded detail (on tap)
                        if isExpanded {
                            Divider().padding(.horizontal, 12)
                            VStack(alignment: .leading, spacing: 4) {
                                Text("Reason: \(reason)").font(.caption).foregroundStyle(.secondary)
                                Text("Impact: \(failureImpact(reason))").font(.caption).foregroundStyle(.secondary)
                                if reason == "Timeout" {
                                    Text("Configured: \(formatDuration(type.timeoutConfiguredSeconds)) \(type.timeoutMode)")
                                        .font(.caption).foregroundStyle(.secondary)
                                    if type.learnedTimeout > 0 {
                                        Text("Observed needed: ~\(formatDuration(type.learnedTimeout))")
                                            .font(.caption).foregroundStyle(.secondary)
                                    }
                                    Text("Suggested retry: \(type.suggestedRetryTimeout > 0 ? formatDuration(type.suggestedRetryTimeout) : formatDuration(LocalMetricTimeoutProfile.maximumTimeout))")
                                        .font(.caption).foregroundStyle(.secondary)
                                }
                            }
                            .padding(.horizontal, 12)
                            .padding(.vertical, 8)
                            .frame(maxWidth: .infinity, alignment: .leading)
                        }
                    }
                    .background(Color(.systemGray6))
                    .cornerRadius(8)
                }
            }
        }
    }

    private func reportDecisionOverviewSection() -> some View {
        reportRemediationSection(
            [
                "Retry the full snapshot with extended timeout.",
                "Save the partial snapshot only if this incomplete report is useful.",
                "Discard snapshot and start a new one.",
                "Disable this metric in Settings if it consistently times out."
            ],
            title: "WHAT YOU CAN DO"
        )
    }

    private func reportRemediationSection(_ items: [String], title: String = "WHAT TO DO") -> some View {
        let isNotes = title == "NOTES"
        return VStack(alignment: .leading, spacing: 6) {
            Text(title)
                .font(isNotes ? .caption : .caption.weight(.semibold))
                .foregroundStyle(isNotes ? Color(.tertiaryLabel) : .secondary)
            VStack(alignment: .leading, spacing: isNotes ? 4 : 8) {
                ForEach(Array(items.enumerated()), id: \.offset) { index, item in
                    HStack(alignment: .top, spacing: 8) {
                        if isNotes {
                            Text("·")
                                .font(.caption2)
                                .foregroundStyle(Color(.tertiaryLabel))
                                .frame(width: 12, alignment: .trailing)
                        } else {
                            Text("\(index + 1).")
                                .font(.caption.weight(.semibold))
                                .foregroundStyle(.secondary)
                                .frame(width: 16, alignment: .trailing)
                        }
                        Text(item)
                            .font(isNotes ? .caption2 : .caption)
                            .foregroundStyle(isNotes ? Color(.secondaryLabel) : .primary)
                            .fixedSize(horizontal: false, vertical: true)
                    }
                }
            }
            .padding(12)
            .frame(maxWidth: .infinity, alignment: .leading)
            .background(Color(.systemGray6))
            .cornerRadius(8)
        }
    }

    private func progressFeedView(_ progress: SnapshotFetchProgress) -> some View {
        VStack(spacing: 12) {
            HStack {
                Text(fetchProgressSummary(progress))
                    .font(.caption.weight(.semibold))
                    .foregroundStyle(.secondary)
                Spacer()
                Text("\(progress.totalRecords) records")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }

            ScrollView {
                VStack(spacing: 8) {
                    ForEach(progress.visibleTypes) { type in
                        HStack(spacing: 12) {
                            if case .fetching = type.status {
                                ProgressView()
                                    .scaleEffect(0.8, anchor: .center)
                                    .frame(width: 20)
                            } else {
                                Image(systemName: type.status.icon)
                                    .font(.subheadline.weight(.semibold))
                                    .frame(width: 20)
                                    .foregroundStyle(colorForStatus(type.status))
                            }

                            VStack(alignment: .leading, spacing: 2) {
                                Text(type.displayName)
                                    .font(.caption)
                                    .lineLimit(1)
                                if !type.blockProgress.isEmpty {
                                    Text(type.blockProgress)
                                        .font(.caption2)
                                        .foregroundStyle(.secondary)
                                        .lineLimit(2)
                                }
                            }
                            .frame(maxWidth: .infinity, alignment: .leading)

                            fetchMetricStatsColumn(type)
                        }
                        .padding(.horizontal, 10)
                        .padding(.vertical, 9)
                        .background(rowBackground(for: type.status))
                        .cornerRadius(6)
                        .overlay {
                            RoundedRectangle(cornerRadius: 6)
                                .stroke(rowBorder(for: type.status), lineWidth: 1)
                        }
                    }
                }
                .frame(maxWidth: .infinity)
            }
        }
        .frame(maxHeight: .infinity)
    }

    private var pendingReportView: some View {
        VStack(spacing: 12) {
            Image(systemName: "doc.text.magnifyingglass")
                .font(.system(size: 36))
                .foregroundStyle(.secondary)
            Text("Report will appear when the snapshot finishes.")
                .font(.caption)
                .foregroundStyle(.secondary)
                .multilineTextAlignment(.center)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }

    private var snapshotSheetTabPicker: some View {
        HStack(spacing: 2) {
            ForEach(SnapshotSheetTab.allCases) { tab in
                let isSelected = snapshotSheetTab == tab
                let isDisabled = tab == .report && !shouldShowFetchReport

                Button {
                    snapshotSheetTab = tab
                } label: {
                    Text(tab.rawValue)
                        .font(.caption.weight(.semibold))
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 8)
                        .foregroundStyle(isDisabled ? .tertiary : (isSelected ? .primary : .secondary))
                        .background(isSelected ? Color(.systemBackground) : Color.clear)
                        .cornerRadius(6)
                }
                .buttonStyle(.plain)
                .disabled(isDisabled)
                .accessibilityLabel(tab.rawValue)
            }
        }
        .padding(2)
        .background(Color(.systemGray5))
        .cornerRadius(8)
    }

    private var sheetOperationHeader: some View {
        VStack(alignment: .center, spacing: 4) {
            HStack(spacing: 8) {
                Image(systemName: "camera.viewfinder")
                    .font(.system(size: 20, weight: .semibold))
                    .foregroundStyle(Color(.secondaryLabel))
                    .frame(width: 22)

                HStack(spacing: 4) {
                    Text("Health data snapshot").font(.headline)

                    if viewModel.snapshotProgress == .complete {
                        Image(systemName: "checkmark.circle.fill")
                            .font(.caption)
                            .foregroundStyle(Color.healthyGreen)
                    } else if viewModel.snapshotProgress == .incomplete || viewModel.snapshotProgress == .requiresResolution {
                        Image(systemName: "exclamationmark.circle.fill")
                            .font(.caption)
                            .foregroundStyle(Color.warningAmber)
                    }
                }
            }

            if let progress = viewModel.fetchProgress {
                Text("\(progress.totalTypeCount) metrics")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }
        }
        .frame(maxWidth: .infinity)
    }

    @ViewBuilder
    private func fetchMetricStatsColumn(_ type: SnapshotFetchProgress.TypeProgress) -> some View {
        if type.recordCount > 0 || type.blockElapsedSeconds > 0 || type.blockSamplesPerSecond > 0 {
            VStack(alignment: .trailing, spacing: 2) {
                if type.recordCount > 0 {
                    Text("\(type.recordCount) records")
                }
                if type.blockElapsedSeconds > 0 {
                    Text(formatDuration(type.blockElapsedSeconds))
                }
                if type.blockSamplesPerSecond > 0 {
                    Text("\(formatRate(type.blockSamplesPerSecond)) rec/s")
                }
            }
            .font(.caption2)
            .foregroundStyle(.secondary)
            .lineLimit(1)
            .frame(minWidth: 82, alignment: .trailing)
        } else if case .failed(let reason) = type.status, reason == "Not authorized" {
            Text("Unavailable")
                .font(.caption2)
                .foregroundStyle(Color.warningAmber)
                .lineLimit(1)
        }
    }

    private func formatRate(_ value: Double) -> String {
        if value >= 1_000 {
            return value.formatted(.number.precision(.fractionLength(0)).grouping(.automatic))
        }
        if value >= 100 {
            return value.formatted(.number.precision(.fractionLength(0)))
        }
        if value >= 10 {
            return value.formatted(.number.precision(.fractionLength(1)))
        }
        return value.formatted(.number.precision(.fractionLength(2)))
    }

    // MARK: - Progress Sheet

    private var progressSheet: some View {
        VStack(spacing: 16) {
            sheetOperationHeader
                .frame(maxWidth: .infinity, alignment: .center)

            if let progress = viewModel.fetchProgress, shouldShowFetchProgress {
                VStack(spacing: 12) {
                    snapshotSheetTabPicker

                    Group {
                        switch snapshotSheetTab {
                        case .progress:
                            progressFeedView(progress)
                        case .report:
                            if shouldShowFetchReport {
                                ScrollView {
                                    fetchReportView(progress)
                                }
                            } else {
                                pendingReportView
                            }
                        }
                    }
                    .frame(maxHeight: .infinity)
                }
                .frame(maxHeight: .infinity)
            }

            if viewModel.snapshotProgress == .idle, let errorMsg = viewModel.snapshotError, !errorMsg.isEmpty {
                VStack(spacing: 12) {
                    HStack {
                        Text("Error details")
                            .font(.caption.weight(.semibold))
                        Spacer()
                        Button {
                            UIPasteboard.general.string = errorMsg
                        } label: {
                            Image(systemName: "doc.on.doc")
                                .font(.caption)
                        }
                        .accessibilityLabel("Copy error details to clipboard")
                    }
                    .foregroundStyle(.secondary)

                    Text(errorMsg)
                        .font(.caption)
                        .textSelection(.enabled)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(12)
                        .background(Color(.systemGray6))
                        .cornerRadius(8)
                }
            }
        }
        .padding(24)
        .sheet(item: $diagnosticReport) { report in
            DiagnosticReportSheet(reportText: report.text)
        }
        .presentationDetents(sheetDetents)
        .presentationDragIndicator(.visible)
        .interactiveDismissDisabled(viewModel.snapshotProgress == .fetching || viewModel.snapshotProgress == .requiresResolution || viewModel.isRequestingAuth)
        .onAppear {
            snapshotSheetTab = .progress
        }
        .onChange(of: viewModel.snapshotProgress) { _, newProgress in
            if newProgress == .incomplete || newProgress == .requiresResolution {
                snapshotSheetTab = .report
            } else if newProgress == .fetching {
                snapshotSheetTab = .progress
            }
        }
    }

    @ViewBuilder
    private var snapshotResultActionsSection: some View {
        let hasAuthorizationIssue = hasAuthorizationFailures(viewModel.fetchProgress)
        let hasTimeoutIssue = hasTimedOutMetrics(viewModel.fetchProgress)

        HStack(spacing: 12) {
            if hasAuthorizationIssue && !viewModel.canRetryWithPermissions {
                Button {
                    if let url = URL(string: UIApplication.openSettingsURLString) {
                        UIApplication.shared.open(url)
                    }
                } label: {
                    Text("Open Settings").frame(maxWidth: .infinity)
                }
                .buttonStyle(.borderedProminent)
                .disabled(viewModel.isCreatingSnapshot || viewModel.isRequestingAuth)
                .accessibilityLabel("Open app settings to restore HealthKit read access")
            } else if viewModel.canRetryWithPermissions || hasTimeoutIssue {
                Button {
                    Task {
                        if viewModel.canRetryWithPermissions {
                            await viewModel.retryWithPermissions(
                                context: modelContext,
                                selectedTypeIDs: appSettings.selectedTypeIDs,
                                adaptiveTimeoutsEnabled: appSettings.adaptiveTimeoutsEnabled
                            )
                        } else {
                            await viewModel.retryFailedMetricsWithExtendedTimeout(
                                context: modelContext,
                                selectedTypeIDs: appSettings.selectedTypeIDs,
                                adaptiveTimeoutsEnabled: appSettings.adaptiveTimeoutsEnabled
                            )
                        }
                    }
                } label: {
                    Text("Retry").frame(maxWidth: .infinity)
                }
                .buttonStyle(.borderedProminent)
                .disabled(viewModel.isCreatingSnapshot || viewModel.isRequestingAuth)
            }

            Button {
                Task { await viewModel.savePartialSnapshot(context: modelContext) }
            } label: {
                Text("Save Partial").frame(maxWidth: .infinity)
            }
            .buttonStyle(.bordered)
            .disabled(viewModel.isCreatingSnapshot)
            .accessibilityLabel("Save partial snapshot")

            Button {
                Task { await viewModel.discardSnapshot(context: modelContext) }
            } label: {
                Text("Discard").foregroundStyle(Color.criticalRed).frame(maxWidth: .infinity)
            }
            .buttonStyle(.bordered)
            .disabled(viewModel.isCreatingSnapshot)
            .accessibilityLabel("Discard snapshot")
        }
    }

    private var ambiguousResolutionActionsSection: some View {
        VStack(spacing: 10) {
            Button {
                Task { await viewModel.treatAmbiguousMetricsAsUnauthorized(context: modelContext) }
            } label: {
                Label("Treat as Unauthorized", systemImage: "lock.slash")
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.borderedProminent)
            .disabled(viewModel.isCreatingSnapshot)
            .accessibilityLabel("Treat missing metrics as unauthorized")

            Button {
                Task { await viewModel.treatAmbiguousMetricsAsDeleted(context: modelContext) }
            } label: {
                Label("Treat as Missing", systemImage: "minus.circle")
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.bordered)
            .disabled(viewModel.isCreatingSnapshot)
            .accessibilityLabel("Treat missing metrics as absent from the current Health store")

            Button {
                Task { await viewModel.discardSnapshot(context: modelContext) }
            } label: {
                Label("Cancel Save", systemImage: "xmark")
                    .foregroundStyle(Color.criticalRed)
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.bordered)
            .disabled(viewModel.isCreatingSnapshot)
            .accessibilityLabel("Cancel saving this snapshot")
        }
    }

    private var sheetDetents: Set<PresentationDetent> {
        [.large]
    }

    // MARK: - Sections

    private var statusSection: some View {
        Section("Status") {
            if let latestArchiveObservation {
                InfoRow(label: "Last Snapshot") {
                    Text(latestArchiveObservation.observedAt, style: .relative)
                        .foregroundStyle(.secondary)
                }
                InfoRow(label: "Records") {
                    Text("\(latestArchiveObservation.visibleRecordCount)")
                        .foregroundStyle(.secondary)
                }
                InfoRow(label: "Types") {
                    Text("\(latestArchiveObservation.trackedTypeCount)")
                        .foregroundStyle(.secondary)
                }
            } else if let latest {
                InfoRow(label: "Last Snapshot") {
                    Text(latest.timestamp, style: .relative)
                        .foregroundStyle(.secondary)
                }
                if latest.snapshotQuality != SnapshotQuality.complete {
                    if latestUnavailableMetricCount > 0 {
                        Label("\(latestUnavailableMetricCount) metric\(latestUnavailableMetricCount == 1 ? "" : "s") unavailable", systemImage: "exclamationmark.triangle")
                            .font(.caption)
                            .foregroundStyle(Color.warningAmber)
                    } else {
                        Label("Incomplete snapshot", systemImage: "exclamationmark.triangle")
                            .font(.caption)
                            .foregroundStyle(Color.warningAmber)
                    }
                }
            } else {
                Label("No snapshots yet", systemImage: "camera.viewfinder")
                    .foregroundStyle(.secondary)
            }

            if latest != nil || latestArchiveObservation != nil {
                InfoRow(label: "Device") {
                    Text(currentDeviceDisplayName)
                        .foregroundStyle(.secondary)
                }
            }

            InfoRow(label: "Monitored Types") {
                Text("\(appSettings.selectedTypeIDs.count)")
                    .foregroundStyle(.secondary)
            }

            if let archiveObservation = latestArchiveObservation {
                InfoRow(label: "Observation ID") {
                    Text("\(archiveObservation.observationID)")
                        .foregroundStyle(.secondary)
                }
            }

            if let archiveCacheError = viewModel.archiveCacheError {
                Label("Archive cache error", systemImage: "exclamationmark.triangle")
                    .font(.caption)
                    .foregroundStyle(Color.criticalRed)
                Text(archiveCacheError)
                    .font(.caption2)
                    .foregroundStyle(.secondary)
                    .lineLimit(2)
            } else if let archiveHealthStatus = viewModel.archiveHealthStatus {
                InfoRow(label: "Archive Cache") {
                    Text(archiveHealthStatus.lastIntegrityStatus == "ok" ? "OK" : archiveHealthStatus.lastIntegrityStatus)
                        .foregroundStyle(archiveHealthStatus.lastIntegrityStatus == "ok" ? Color.healthyGreen : Color.warningAmber)
                }
                InfoRow(label: "Cache Schema") {
                    Text("\(archiveHealthStatus.cacheSchemaVersion)")
                        .foregroundStyle(.secondary)
                }
            } else {
                Label("Archive cache not built", systemImage: "externaldrive.badge.questionmark")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }

            if let archiveDelta = latestArchiveChangeCount {
                InfoRow(label: "Changes vs Previous") {
                    Text(archiveDelta == 0 ? "None" : "\(archiveDelta) records")
                        .foregroundStyle(archiveDelta == 0 ? Color.healthyGreen : Color.warningAmber)
                }
            } else if let latest, let previous {
                let delta = viewModel.totalChanges(latest: latest, previous: previous)
                InfoRow(label: "Changes vs Previous") {
                    Text(delta == 0 ? "None" : "\(delta) records")
                        .foregroundStyle(delta == 0 ? Color.healthyGreen : Color.warningAmber)
                }
            }
        }
    }

    @ViewBuilder
    private var anomalySummarySection: some View {
        AnomalySummarySection()
    }

    private var actionsSection: some View {
        Section("Actions") {
            Button {
                Task { await viewModel.requestAuthorization() }
            } label: {
                HStack {
                    Label("Request Health Access", systemImage: "heart.text.square")
                    Spacer()
                    if viewModel.isRequestingAuth { ProgressView() }
                }
            }
            .disabled(viewModel.isRequestingAuth)
            .accessibilityLabel("Request HealthKit read authorization")

            Button {
                Task {
                    await viewModel.createSnapshot(
                        context: modelContext,
                        selectedTypeIDs: appSettings.selectedTypeIDs,
                        adaptiveTimeoutsEnabled: appSettings.adaptiveTimeoutsEnabled
                    )
                }
            } label: {
                Label("Create Snapshot", systemImage: "camera.viewfinder")
            }
            .disabled(viewModel.isCreatingSnapshot)
            .accessibilityLabel("Create a new data snapshot")
        }
    }
}

private enum SnapshotSheetTab: String, CaseIterable, Identifiable {
    case progress = "Progress"
    case report = "Result"

    var id: Self { self }
}

private struct DiagnosticReport: Identifiable {
    let id = UUID()
    let text: String
}

// Avoids LabeledContent's TableRowContent ambiguity in List/Section contexts.
private struct InfoRow<Content: View>: View {
    let label: String
    @ViewBuilder let content: () -> Content

    var body: some View {
        HStack {
            Text(label)
            Spacer()
            content()
        }
    }
}

private struct ReportActionRow: View {
    let label: String
    let value: String
    let systemImage: String
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            HStack(spacing: 10) {
                Label(label, systemImage: systemImage)
                    .font(.subheadline)
                    .labelStyle(.titleAndIcon)
                Spacer()
                HStack(spacing: 6) {
                    Text(value)
                        .font(.subheadline.weight(.semibold))
                        .foregroundStyle(.secondary)
                    Image(systemName: "chevron.right")
                        .font(.caption2.weight(.semibold))
                        .foregroundStyle(.tertiary)
                }
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 10)
            .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
        .accessibilityLabel("\(label), \(value)")
    }
}

private struct ReportRow: View {
    let label: String
    let value: String
    var valueColor: Color = .primary

    var body: some View {
        HStack {
            Text(label)
                .font(.subheadline)
            Spacer()
            Text(value)
                .font(.subheadline.weight(.semibold))
                .foregroundStyle(valueColor)
        }
        .padding(.horizontal, 12)
        .padding(.vertical, 10)
    }
}

private struct DiagnosticReportSheet: View {
    @Environment(\.dismiss) private var dismiss
    let reportText: String
    @State private var didCopy = false

    var body: some View {
        NavigationStack {
            ScrollView {
                Text(reportText)
                    .font(.system(.caption, design: .monospaced))
                    .textSelection(.enabled)
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding(16)
            }
            .background(Color(.systemGroupedBackground))
            .navigationTitle("Diagnostics")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Done") {
                        dismiss()
                    }
                }
                ToolbarItem(placement: .primaryAction) {
                    Button {
                        copyDiagnostics()
                    } label: {
                        Label(didCopy ? "Copied" : "Copy", systemImage: didCopy ? "checkmark" : "doc.on.doc")
                    }
                    .accessibilityLabel(didCopy ? "Diagnostics copied" : "Copy diagnostics")
                }
            }
        }
        .presentationDetents([.large])
        .presentationDragIndicator(.visible)
    }

    private func copyDiagnostics() {
        UIPasteboard.general.string = reportText
        withAnimation(.snappy) {
            didCopy = true
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            withAnimation(.snappy) {
                didCopy = false
            }
        }
    }
}

private struct AnomalySummarySection: View {
    @Environment(\.modelContext) private var modelContext
    @Query(filter: #Predicate<AnomalyRecord> { !$0.isResolved })
    private var unresolved: [AnomalyRecord]

    private var criticalCount: Int { unresolved.filter { $0.severityRaw == Severity.critical.rawValue }.count }
    private var warningCount:  Int { unresolved.filter { $0.severityRaw == Severity.warning.rawValue }.count }
    private var disappearedRecords: [AnomalyRecord] {
        unresolved
            .filter { $0.anomalyType == .deletion }
            .sorted { $0.detectedAt > $1.detectedAt }
    }

    private func confirmAuthorizationChange(for anomaly: AnomalyRecord) {
        defer {
            anomaly.isResolved = true
            try? modelContext.save()
        }

        guard let typeIdentifier = anomaly.typeIdentifier else { return }
        let snapshotID = anomaly.snapshotID

        let descriptor = FetchDescriptor<HealthSnapshot>(
            predicate: #Predicate<HealthSnapshot> { $0.id == snapshotID }
        )
        guard let snapshot = try? modelContext.fetch(descriptor).first,
              let typeCount = snapshot.typeCounts?.first(where: { $0.typeIdentifier == typeIdentifier }) else {
            return
        }

        typeCount.count = -1
        typeCount.contentHash = ""
        typeCount.earliestDate = nil
        typeCount.latestDate = nil
        typeCount.quality = .unauthorized
        typeCount.yearlyCounts?.removeAll()
        snapshot.snapshotQuality = HealthKitService.shared.deriveSnapshotQuality(from: snapshot.typeCounts ?? [])
    }

    var body: some View {
        if !unresolved.isEmpty {
            Section("Change Review") {
                if criticalCount > 0 {
                    Label("\(criticalCount) critical \(criticalCount == 1 ? "change" : "changes")",
                          systemImage: "exclamationmark.circle.fill")
                        .foregroundStyle(Color.criticalRed)
                }
                if warningCount > 0 {
                    Label("\(warningCount) \(warningCount == 1 ? "warning" : "warnings")",
                          systemImage: "exclamationmark.triangle.fill")
                        .foregroundStyle(Color.warningAmber)
                }
                if !disappearedRecords.isEmpty {
                    VStack(alignment: .leading, spacing: 8) {
                        Label("Records missing from current view", systemImage: "eye.slash")
                            .font(.subheadline.weight(.semibold))
                            .foregroundStyle(Color.criticalRed)
                        Text("Check Health read access for the affected metrics first. If you intentionally revoked access, confirm that here; otherwise treat this as a change that needs external review.")
                            .font(.caption)
                            .foregroundStyle(.secondary)
                        ForEach(disappearedRecords.prefix(3)) { anomaly in
                            VStack(alignment: .leading, spacing: 6) {
                                Text(anomaly.message)
                                    .font(.caption)
                                    .foregroundStyle(.secondary)
                                Button("Confirm Authorization Change") {
                                    confirmAuthorizationChange(for: anomaly)
                                }
                                .buttonStyle(.bordered)
                                .controlSize(.small)
                            }
                        }
                    }
                    .padding(.vertical, 4)
                }
            }
        }
    }
}

extension Bundle {
    var appVersion: String {
        guard let version = infoDictionary?["CFBundleShortVersionString"] as? String else {
            return "unknown"
        }
        guard let build = infoDictionary?["CFBundleVersion"] as? String else {
            return version
        }
        return "\(version)(\(build))"
    }
}

#Preview {
    DashboardView()
        .modelContainer(for: [HealthSnapshot.self, TypeCount.self, YearlyCount.self, HealthRecord.self, TypeDistributionBin.self], inMemory: true)
        .environment(AppSettings())
}
