import SwiftUI

struct EditableDraftCard: View {
    @Bindable var vm: AssistantViewModel
    @State private var isEditing = false

    private var draft: FitnessDraft? { vm.currentDraft }

    var body: some View {
        if let draft {
            VStack(alignment: .leading, spacing: 10) {
                // Header
                HStack {
                    Image(systemName: "doc.text.fill")
                        .foregroundStyle(Color.accentWarm)
                    Text("Draft")
                        .font(.subheadline.weight(.semibold))
                        .foregroundStyle(Color.textPrimary)
                    Spacer()
                    Button {
                        withAnimation(.easeInOut(duration: 0.2)) { isEditing.toggle() }
                    } label: {
                        Text(isEditing ? "Done" : "Edit")
                            .font(.caption.weight(.semibold))
                            .foregroundStyle(Color.accentWarm)
                    }
                }

                // Food name
                if isEditing {
                    TextField("Food name", text: binding(\.foodName))
                        .font(.headline)
                        .foregroundStyle(Color.textPrimary)
                        .textFieldStyle(.plain)
                } else {
                    Text(draft.foodName)
                        .font(.headline)
                        .foregroundStyle(Color.textPrimary)
                }

                // Macros — editable or static
                if isEditing {
                    LazyVGrid(columns: [
                        GridItem(.flexible()),
                        GridItem(.flexible()),
                        GridItem(.flexible()),
                    ], spacing: 10) {
                        editableMacro("Cal", value: binding(\.calories), color: .emerald)
                        editableMacro("Protein", value: binding(\.protein), color: .macroProtein)
                        editableMacro("Carbs", value: binding(\.carbs), color: .macroCarbs)
                        editableMacro("Fat", value: binding(\.fat), color: .macroFat)
                        editableMacro("Sugar", value: binding(\.sugar), color: .orange)
                        editableMacro("Fiber", value: binding(\.fiber), color: .green)
                    }
                } else {
                    HStack(spacing: 16) {
                        miniStat("Cal", value: Int(draft.calories))
                        miniStat("P", value: Int(draft.protein))
                        miniStat("C", value: Int(draft.carbs))
                        miniStat("F", value: Int(draft.fat))
                    }
                }

                // Meal type + quantity
                if isEditing {
                    HStack(spacing: 12) {
                        // Meal picker
                        Menu {
                            ForEach(MealType.allCases, id: \.self) { meal in
                                Button {
                                    vm.currentDraft?.mealType = meal.rawValue
                                } label: {
                                    Label(meal.displayName, systemImage: meal.icon)
                                }
                            }
                        } label: {
                            HStack(spacing: 4) {
                                Image(systemName: MealType(rawValue: draft.mealType)?.icon ?? "fork.knife")
                                    .font(.caption)
                                Text(draft.mealType.capitalized)
                                    .font(.caption.weight(.medium))
                                Image(systemName: "chevron.up.chevron.down")
                                    .font(.system(size: 8))
                            }
                            .foregroundStyle(Color.accentWarm)
                            .padding(.horizontal, 10)
                            .padding(.vertical, 6)
                            .background(Color.accentWarm.opacity(0.1))
                            .clipShape(Capsule())
                        }

                        // Quantity
                        HStack(spacing: 6) {
                            Text("Qty:")
                                .font(.caption)
                                .foregroundStyle(Color.textSecondary)
                            TextField("1.0", text: quantityBinding)
                                .font(.caption.weight(.semibold).monospacedDigit())
                                .foregroundStyle(Color.textPrimary)
                                .keyboardType(.decimalPad)
                                .frame(width: 40)
                                .multilineTextAlignment(.center)
                            Text(draft.unit)
                                .font(.caption)
                                .foregroundStyle(Color.textTertiary)
                        }

                        Spacer()
                    }
                } else {
                    HStack {
                        Text("\(draft.mealType.capitalized) \u{2022} \(draft.quantity, specifier: "%.1f") \(draft.unit)")
                            .font(.caption)
                            .foregroundStyle(Color.textSecondary)
                        Spacer()
                    }
                }

                // Add button
                Button {
                    isEditing = false
                    Task { await vm.applyDraft() }
                } label: {
                    Text("Add it")
                        .font(.subheadline.weight(.semibold))
                        .foregroundStyle(.white)
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 10)
                        .background(Color.emerald)
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                }
            }
            .padding()
            .background(Color.surfaceCard)
            .clipShape(RoundedRectangle(cornerRadius: 14))
            .shadow(color: .black.opacity(0.06), radius: 6, y: 2)
            .padding(.horizontal, 12)
        }
    }

    // MARK: - Editable macro cell

    private func editableMacro(_ label: String, value: Binding<Double>, color: Color) -> some View {
        VStack(spacing: 2) {
            TextField("0", text: doubleBinding(value))
                .font(.caption.weight(.bold).monospacedDigit())
                .foregroundStyle(color)
                .multilineTextAlignment(.center)
                .keyboardType(.numberPad)
                .frame(height: 24)
            Text(label)
                .font(.system(size: 9).weight(.medium))
                .foregroundStyle(Color.textTertiary)
        }
        .padding(.vertical, 4)
        .background(color.opacity(0.06))
        .clipShape(RoundedRectangle(cornerRadius: 6))
    }

    // MARK: - Static mini stat

    private func miniStat(_ label: String, value: Int) -> some View {
        VStack(spacing: 2) {
            Text("\(value)")
                .font(.caption.weight(.bold).monospacedDigit())
                .foregroundStyle(Color.textPrimary)
            Text(label)
                .font(.system(size: 9).weight(.medium))
                .foregroundStyle(Color.textTertiary)
        }
    }

    // MARK: - Bindings into vm.currentDraft

    private func binding(_ keyPath: WritableKeyPath<FitnessDraft, String>) -> Binding<String> {
        Binding(
            get: { vm.currentDraft?[keyPath: keyPath] ?? "" },
            set: { vm.currentDraft?[keyPath: keyPath] = $0 }
        )
    }

    private func binding(_ keyPath: WritableKeyPath<FitnessDraft, Double>) -> Binding<Double> {
        Binding(
            get: { vm.currentDraft?[keyPath: keyPath] ?? 0 },
            set: { vm.currentDraft?[keyPath: keyPath] = $0 }
        )
    }

    private func doubleBinding(_ binding: Binding<Double>) -> Binding<String> {
        Binding(
            get: { String(Int(binding.wrappedValue)) },
            set: { binding.wrappedValue = Double($0) ?? 0 }
        )
    }

    private var quantityBinding: Binding<String> {
        Binding(
            get: { String(format: "%.1f", vm.currentDraft?.quantity ?? 1) },
            set: { vm.currentDraft?.quantity = Double($0) ?? 1 }
        )
    }
}
