// LoopFollow
// SensorAgeAlarmEditor.swift

import SwiftUI

struct SensorAgeAlarmEditor: View {
    @Binding var alarm: Alarm

    private var lifetimeDays: Int {
        alarm.sensorLifetimeDays ?? 10
    }

    private var lifetimeBinding: Binding<Int?> {
        Binding(
            get: { alarm.sensorLifetimeDays ?? 10 },
            set: { alarm.sensorLifetimeDays = $0 }
        )
    }

    var body: some View {
        Group {
            InfoBanner(
                text: String(localized: "Warn me before the sensor’s \(lifetimeDays)-day change-over."),
                alarmType: alarm.type
            )

            AlarmGeneralSection(alarm: $alarm)

            AlarmStepperSection(
                header: String(localized: "Sensor Lifetime"),
                footer: String(localized: "Number of days your CGM sensor lasts (e.g. 10 for Dexcom G6, 15 for G7 15-day)."),
                title: String(localized: "Lifetime"),
                range: 7 ... 15,
                step: 1,
                unitLabel: String(localized: "days"),
                value: lifetimeBinding
            )

            AlarmStepperSection(
                header: String(localized: "Early Reminder"),
                footer: String(localized: "Number of hours before the \(lifetimeDays)-day mark that the alert will fire."),
                title: String(localized: "Reminder Time"),
                range: 1 ... 24,
                step: 1,
                unitLabel: String(localized: "hours"),
                value: $alarm.threshold
            )

            AlarmActiveSection(alarm: $alarm)
            AlarmAudioSection(alarm: $alarm)
            AlarmSnoozeSection(alarm: $alarm)
        }
    }
}
