import SwiftUI

struct GoalSettingDetailView: View {
    @State private var selectedGoal = "备考考试"
    let goals = [("person.crop.circle.fill","备考考试","公考、考研、考证等"),("briefcase.fill","职业技能","编程、设计、产品等"),("books.vertical.fill","通识学习","扩充知识面"),("target","自定义","设定自己的目标")]
    @State private var dailyMins = "30 分钟"
    let times = ["15 分钟","30 分钟","1 小时","不限制"]
    @State private var isSaving = false

    var body: some View {
        ZStack {
            Color.zxBg0.ignoresSafeArea()
            ScrollView {
                VStack(spacing: 16) {
                    VStack(alignment: .leading, spacing: 10) {
                        Text("学习目标").font(.system(size: 12, weight: .semibold)).foregroundColor(Color.zxF035)
                        ForEach(goals, id: \.1) { g in let sel = selectedGoal == g.1
                            Button { selectedGoal = g.1 } label: {
                                HStack(spacing: 12) {
                                    Image(systemName: g.0).font(.system(size: 20)).foregroundColor(sel ? Color.zxPurple : Color.zxF05).frame(width: 44, height: 44).background(sel ? Color(hex: "#7C6EFA", opacity: 0.15) : Color.zxFill005).clipShape(RoundedRectangle(cornerRadius: 12))
                                    VStack(alignment: .leading, spacing: 2) { Text(g.1).font(.system(size: 15, weight: .semibold)).foregroundColor(sel ? Color.zxPurple : Color.zxF0); Text(g.2).font(.system(size: 12)).foregroundColor(Color.zxF04) }
                                    Spacer()
                                    Circle().stroke(sel ? Color.zxPurple : Color(hex: "#FFFFFF", opacity: 0.2), lineWidth: 2).frame(width: 22, height: 22).overlay { if sel { Circle().fill(Color.zxPurple).frame(width: 12, height: 12) } }
                                }.padding(14).background(sel ? Color(hex: "#7C6EFA", opacity: 0.08) : Color.zxFill003).clipShape(RoundedRectangle(cornerRadius: 16))
                            }.foregroundColor(.primary)
                        }
                    }
                    VStack(alignment: .leading, spacing: 10) {
                        Text("每日学习时间").font(.system(size: 12, weight: .semibold)).foregroundColor(Color.zxF035)
                        HStack(spacing: 8) { ForEach(times, id: \.self) { t in let sel = dailyMins == t; Button { dailyMins = t } label: { Text(t).font(.system(size: 12)).fontWeight(sel ? .semibold : .regular).foregroundColor(sel ? Color.zxPurple : Color.zxF05).frame(maxWidth: .infinity).frame(height: 40).background(sel ? Color(hex: "#7C6EFA", opacity: 0.1) : Color.zxFill003).clipShape(RoundedRectangle(cornerRadius: 12)) }.foregroundColor(.primary) } }
                    }
                    Button {
                        Task {
                            isSaving = true
                            _ = try? await UserService.shared.updateProfileDetail(UpdateProfileDataRequest(
                                learningIdentity: nil, learningDirection: nil, bio: nil, currentGoal: selectedGoal
                            ))
                            isSaving = false
                        }
                    } label: {
                        HStack(spacing: 8) {
                            if isSaving { ProgressView().tint(.white) }
                            Text("保存").font(.system(size: 14, weight: .bold))
                        }
                        .foregroundColor(.white).frame(maxWidth: .infinity).frame(height: 52)
                        .background(isSaving ? AnyView(Color.gray) : AnyView(ZXGradient.ctaPurple))
                        .clipShape(RoundedRectangle(cornerRadius: 16))
                    }
                    .disabled(isSaving)
                }.padding(.horizontal, 20).padding(.top, 8).padding(.bottom, 80)
            }.scrollIndicators(.hidden)
        }.navigationBarTitleDisplayMode(.inline).animatedTabBarHide().toolbarBackground(.hidden, for: .navigationBar)
    }
}
