//
//  AlarmLimitationsBanner.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 2/2/26.
//

import SwiftUI
import Bedrock
import Foundation

/// Banner explaining background alarm limitations and mitigation.
struct AlarmLimitationsBanner: View {
    @AppStorage(ClockStyle.appStorageKey) private var clockStyleData: Data = Data()
    
    var body: some View {
        if isKeepAwakeEnabled {
            EmptyView()
        } else {
            SettingsCard(backgroundColor: AppSurface.card, borderColor: AppBorder.subtle) {
                VStack(alignment: .leading, spacing: Design.Spacing.xSmall) {
                    HStack(spacing: Design.Spacing.xSmall) {
                        Image(systemName: "exclamationmark.triangle.fill")
                            .foregroundStyle(AppStatus.warning)
                        Text("Alarm reliability")
                            .typography(.body)
                            .fontWeight(.semibold)
                            .foregroundStyle(AppTextColors.primary)
                    }
                    
                    Text("iOS only allows notification sounds when the app is backgrounded. For a full alarm sound and screen, keep TheNoiseClock open in the foreground.")
                        .typography(.caption)
                        .foregroundStyle(AppTextColors.secondary)
                    
                    Text("Tip: Use the Keep Awake prompt to keep the app on-screen while alarms are active.")
                        .typography(.caption)
                        .foregroundStyle(AppTextColors.secondary)
                }
                .padding(Design.Spacing.medium)
                .frame(maxWidth: .infinity, alignment: .leading)
                .background(AppSurface.primary)
            }
        }
    }
    
    private var isKeepAwakeEnabled: Bool {
        guard let decoded = try? JSONDecoder().decode(ClockStyle.self, from: clockStyleData) else {
            return ClockStyle().keepAwake
        }
        return decoded.keepAwake
    }
}

#Preview {
    AlarmLimitationsBanner()
        .padding()
        .background(AppSurface.primary)
}
