import SwiftUI

struct MealWindowsStepView: View {
    @Binding var selection: MealWindows
    var onNext: () -> Void

    var body: some View {
        VStack(spacing: 24) {
            Text("meal_windows_title")
                .font(.mealMoodH1)
                .foregroundColor(.mealMoodTextPrimary)
                .multilineTextAlignment(.leading)
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding(.horizontal, 24)
                .padding(.top, 12)

            VStack(spacing: 12) {
                ForEach(MealWindows.allCases, id: \.self) { option in
                    SelectionCard(
                        icon: option.icon,
                        title: optionTitle(option),
                        isSelected: selection == option
                    ) {
                        withAnimation(.spring(response: 0.3)) {
                            selection = option
                        }
                        HapticManager.shared.selection()
                    }
                }
            }
            .padding(.horizontal, 24)

            PrimaryButton(title: String(localized: "onboarding_continue"), action: onNext)
                .padding(.horizontal, 24)
                .padding(.top, 8)
                .padding(.bottom, 40)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
    }

    private func optionTitle(_ option: MealWindows) -> String {
        switch option {
        case .dinnerOnly: return "🌙 \(String(localized: "meal_windows_dinner_only"))"
        case .lunchOnly: return "☀️ \(String(localized: "meal_windows_lunch_only"))"
        case .both: return "🌞🌙 \(String(localized: "meal_windows_both"))"
        }
    }
}

struct SelectionCard: View {
    let icon: String
    let title: String
    let isSelected: Bool
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            HStack {
                Text(title)
                    .font(.mealMoodBody)
                    .foregroundColor(.mealMoodTextPrimary)
                Spacer()
                Image(systemName: isSelected ? "checkmark.circle.fill" : "circle")
                    .foregroundColor(isSelected ? .mealMoodCoral : Color(hex: "#C4C4C4"))
                    .font(.system(size: 22))
            }
            .padding(16)
            .background(Color.mealMoodSurface)
            .cornerRadius(14)
            .overlay(
                RoundedRectangle(cornerRadius: 14)
                    .stroke(isSelected ? Color.mealMoodCoral : Color(hex: "#F0F0F0"), lineWidth: isSelected ? 2 : 1)
            )
        }
    }
}
