import SwiftUI

struct FitnessTabView: View {
    @State private var selectedSubTab = 0

    var body: some View {
        NavigationStack {
        VStack(spacing: 0) {
            // Sub-tab selector
            HStack(spacing: 0) {
                ForEach(Array(fitnessSubTabs.enumerated()), id: \.offset) { index, tab in
                    Button {
                        withAnimation(.easeInOut(duration: 0.2)) {
                            selectedSubTab = index
                        }
                    } label: {
                        Text(tab)
                            .font(.subheadline.weight(selectedSubTab == index ? .semibold : .regular))
                            .foregroundStyle(selectedSubTab == index ? Color.accentWarm : Color.textSecondary)
                            .padding(.vertical, 10)
                            .padding(.horizontal, 16)
                            .background {
                                if selectedSubTab == index {
                                    Capsule()
                                        .fill(Color.accentWarm.opacity(0.12))
                                }
                            }
                    }
                }
            }
            .padding(.horizontal)
            .padding(.top, 8)

            // Content — tap tabs to switch (no page swipe, preserves swipe-to-delete)
            Group {
                switch selectedSubTab {
                case 0: TodayView()
                case 1: TemplatesView()
                case 2: GoalsView()
                case 3: FoodLibraryView()
                default: TodayView()
                }
            }
            .animation(.easeInOut(duration: 0.2), value: selectedSubTab)
        }
        .background(Color.canvas)
        .navigationBarHidden(true)
        }
    }

    private var fitnessSubTabs: [String] {
        ["Today", "Templates", "Goals", "Foods"]
    }
}
