import SwiftUI

struct TodayView: View {
    @State private var vm = TodayViewModel()

    var body: some View {
        ScrollView {
            VStack(spacing: 16) {
                // Date selector
                dateSelector

                // Macro summary
                macroSummary

                // Meal sections
                if vm.entries.isEmpty && !vm.isLoading {
                    EmptyStateView(
                        icon: "fork.knife",
                        title: "No entries yet",
                        subtitle: "Tap + to log your first meal"
                    )
                } else {
                    ForEach(vm.mealGroups, id: \.0) { mealType, entries in
                        MealSectionView(
                            mealType: mealType,
                            entries: entries,
                            onDelete: { entry in
                                Task { await vm.deleteEntry(entry) }
                            }
                        )
                    }
                }

                Spacer(minLength: 80)
            }
            .padding(.top, 8)
        }
        .refreshable {
            await vm.load()
        }
        .background(Color.canvas)
        .task {
            await vm.load()
        }
        .onChange(of: vm.selectedDate) {
            Task { await vm.load() }
        }
    }

    private var dateSelector: some View {
        HStack {
            Button {
                vm.goToPreviousDay()
            } label: {
                Image(systemName: "chevron.left")
                    .font(.title3.weight(.medium))
                    .foregroundStyle(Color.accentWarm)
            }

            Spacer()

            Button {
                vm.goToToday()
            } label: {
                Text(vm.displayDateString)
                    .font(.headline)
                    .foregroundStyle(Color.textPrimary)
            }

            Spacer()

            Button {
                vm.goToNextDay()
            } label: {
                Image(systemName: "chevron.right")
                    .font(.title3.weight(.medium))
                    .foregroundStyle(Color.accentWarm)
            }
        }
        .padding(.horizontal, 20)
        .padding(.vertical, 8)
    }

    private var macroSummary: some View {
        VStack(spacing: 16) {
            HStack(spacing: 24) {
                MacroRingWithLabel(
                    consumed: vm.totalCalories,
                    goal: vm.calorieGoal,
                    label: "kcal",
                    color: .emerald,
                    size: 90,
                    lineWidth: 9
                )

                VStack(spacing: 10) {
                    MacroBar(
                        label: "Protein",
                        consumed: vm.totalProtein,
                        goal: vm.proteinGoal,
                        color: .macroProtein
                    )
                    MacroBar(
                        label: "Carbs",
                        consumed: vm.totalCarbs,
                        goal: vm.carbsGoal,
                        color: .macroCarbs
                    )
                    MacroBar(
                        label: "Fat",
                        consumed: vm.totalFat,
                        goal: vm.fatGoal,
                        color: .macroFat
                    )
                    MacroBar(
                        label: "Sugar",
                        consumed: vm.totalSugar,
                        goal: vm.sugarGoal,
                        color: .orange
                    )
                    MacroBar(
                        label: "Fiber",
                        consumed: vm.totalFiber,
                        goal: vm.fiberGoal,
                        color: .green
                    )
                }
            }
        }
        .padding(16)
        .background(Color.surfaceCard)
        .clipShape(RoundedRectangle(cornerRadius: 14))
        .shadow(color: .black.opacity(0.04), radius: 6, y: 2)
        .padding(.horizontal)
    }
}
