import SwiftUI

struct GoalsView: View {
    @State private var viewModel = GoalsViewModel()

    var body: some View {
        ScrollView {
            if viewModel.isLoading {
                LoadingView(message: "Loading goals...")
                    .frame(height: 300)
            } else {
                VStack(spacing: 20) {
                    // Header
                    Text("Your daily targets")
                        .font(.headline)
                        .foregroundStyle(Color.text1)
                        .frame(maxWidth: .infinity, alignment: .leading)

                    // Goals cards
                    goalCard(
                        label: "Calories",
                        value: viewModel.goal.calories,
                        unit: "kcal",
                        icon: "flame.fill",
                        color: .caloriesColor
                    )

                    goalCard(
                        label: "Protein",
                        value: viewModel.goal.protein,
                        unit: "g",
                        icon: "circle.hexagonpath.fill",
                        color: .proteinColor
                    )

                    goalCard(
                        label: "Carbs",
                        value: viewModel.goal.carbs,
                        unit: "g",
                        icon: "bolt.fill",
                        color: .carbsColor
                    )

                    goalCard(
                        label: "Fat",
                        value: viewModel.goal.fat,
                        unit: "g",
                        icon: "drop.fill",
                        color: .fatColor
                    )

                    if let sugar = viewModel.goal.sugar, sugar > 0 {
                        goalCard(
                            label: "Sugar",
                            value: sugar,
                            unit: "g",
                            icon: "cube.fill",
                            color: .sugarColor
                        )
                    }

                    if let fiber = viewModel.goal.fiber, fiber > 0 {
                        goalCard(
                            label: "Fiber",
                            value: fiber,
                            unit: "g",
                            icon: "leaf.fill",
                            color: .fiberColor
                        )
                    }

                    // Info note
                    HStack(spacing: 8) {
                        Image(systemName: "info.circle")
                            .foregroundStyle(Color.text4)
                        Text("Goals can be updated from the web app")
                            .font(.caption)
                            .foregroundStyle(Color.text3)
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding(.top, 8)
                }
                .padding(16)
            }

            if let error = viewModel.errorMessage {
                ErrorBanner(message: error) {
                    Task { await viewModel.load() }
                }
                .padding(16)
            }
        }
        .refreshable {
            await viewModel.load()
        }
        .task {
            await viewModel.load()
        }
    }

    private func goalCard(label: String, value: Double, unit: String, icon: String, color: Color) -> some View {
        HStack(spacing: 16) {
            ZStack {
                RoundedRectangle(cornerRadius: 12)
                    .fill(color.opacity(0.1))
                    .frame(width: 48, height: 48)

                Image(systemName: icon)
                    .font(.title3)
                    .foregroundStyle(color)
            }

            VStack(alignment: .leading, spacing: 2) {
                Text(label)
                    .font(.subheadline.weight(.medium))
                    .foregroundStyle(Color.text2)

                Text("Daily target")
                    .font(.caption)
                    .foregroundStyle(Color.text4)
            }

            Spacer()

            HStack(alignment: .firstTextBaseline, spacing: 2) {
                Text("\(Int(value))")
                    .font(.title2.weight(.bold))
                    .foregroundStyle(Color.text1)
                Text(unit)
                    .font(.subheadline)
                    .foregroundStyle(Color.text3)
            }
        }
        .padding(16)
        .background(Color.surface)
        .clipShape(RoundedRectangle(cornerRadius: 14))
        .shadow(color: .black.opacity(0.03), radius: 4, y: 2)
    }
}
