import SwiftUI
import SwiftData

struct MeView: View {
    @Environment(\.modelContext) private var ctx
    @Query private var profiles: [UserProfile]
    @Query private var customMetrics: [CustomMonitorMetric]

    @State private var downloadService = ModelDownloadService.shared
    @State private var appLock = AppLock.shared
    @State private var lang = LanguageManager.shared
    @State private var fontScale = FontScaleManager.shared
    // key 必须与 AppLock.enabledKey 一致。
    @AppStorage("faceIDLockEnabled") private var lockEnabled = false

    private var profile: UserProfile? { profiles.first }

    /// 真实读取 Bundle 版本号,与「关于」页保持一致。
    private var appVersionText: String {
        let short = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.1"
        return "v\(short)"
    }

    var body: some View {
        NavigationStack {
            ScrollView {
                VStack(spacing: 12) {
                    HStack {
                        Text("我的")
                            .font(.tjTitle())
                            .foregroundStyle(Tj.Palette.text)
                        Spacer()
                    }
                    .padding(.top, 4)
                    .padding(.bottom, 6)

                    profileCard
                    customMetricsCard
                    modelManagementCard
                    inferenceEngineCard
                    languageCard
                    fontScaleCard
                    faceIDCard
                    NavigationLink {
                        AboutView()
                    } label: {
                        settingsCard(title: String(appLoc: "关于"),
                                     detail: appVersionText,
                                     icon: "info.circle")
                    }
                    .buttonStyle(.plain)
                }
                .padding(.horizontal, 16)
                .padding(.vertical, 20)
            }
            .background(Tj.Palette.sand.ignoresSafeArea())
            // 标题改用内容文字渲染(与 主页/记录/趋势 一致),不走 .navigationTitle:
            // 大标题导航栏在真机状态栏区域会露出系统白底,破坏全 App 的沙色背景。
            .onAppear {
                if profiles.isEmpty {
                    _ = UserProfileStore.loadOrCreate(in: ctx)
                }
                downloadService.refreshStates()
                appLock.refreshAvailability()
            }
        }
    }

    // MARK: - Cards

    private var profileCard: some View {
        NavigationLink {
            ProfileEditView()
        } label: {
            HStack(spacing: 12) {
                ZStack {
                    Circle()
                        .fill(Tj.Palette.amber.opacity(0.25))
                    Image(systemName: "person.crop.circle.fill")
                        .font(.tjScaled( 22))
                        .foregroundStyle(Tj.Palette.ink)
                }
                .frame(width: 44, height: 44)

                VStack(alignment: .leading, spacing: 2) {
                    Text("个人资料")
                        .font(.tjScaled( 15, weight: .semibold))
                        .foregroundStyle(Tj.Palette.text)
                    Text(profileLine)
                        .font(.tjScaled( 12))
                        .foregroundStyle(Tj.Palette.text3)
                        .lineLimit(1)
                }
                Spacer()
                Image(systemName: "chevron.right")
                    .font(.tjScaled( 13, weight: .medium))
                    .foregroundStyle(Tj.Palette.text3)
            }
            .padding(14)
            .tjCard()
        }
        .buttonStyle(.plain)
    }

    private var customMetricsCard: some View {
        NavigationLink {
            CustomMetricsListView()
        } label: {
            HStack(spacing: 12) {
                ZStack {
                    Circle()
                        .fill(customMetrics.isEmpty ? Tj.Palette.sand2 : Tj.Palette.leafSoft)
                    Image(systemName: "slider.horizontal.3")
                        .font(.tjScaled( 18))
                        .foregroundStyle(customMetrics.isEmpty ? Tj.Palette.text2 : Tj.Palette.ink)
                }
                .frame(width: 44, height: 44)

                VStack(alignment: .leading, spacing: 2) {
                    Text("自定义指标")
                        .font(.tjScaled( 15, weight: .semibold))
                        .foregroundStyle(Tj.Palette.text)
                    Text(customMetricsLine)
                        .font(.tjScaled( 12))
                        .foregroundStyle(Tj.Palette.text3)
                        .lineLimit(1)
                }
                Spacer()
                Image(systemName: "chevron.right")
                    .font(.tjScaled( 13, weight: .medium))
                    .foregroundStyle(Tj.Palette.text3)
            }
            .padding(14)
            .tjCard()
        }
        .buttonStyle(.plain)
    }

    private var customMetricsLine: String {
        if customMetrics.isEmpty { return String(appLoc: "添加你自己的长期监测项") }
        return String(appLoc: "\(customMetrics.count) 项")
    }

    private var modelManagementCard: some View {
        NavigationLink {
            ModelManagementView()
        } label: {
            settingsCard(title: String(appLoc: "模型管理"), detail: modelDetail, icon: "cpu")
        }
        .buttonStyle(.plain)
    }

    private var modelDetail: String {
        let states = downloadService.states
        if ModelKind.userFacing.allSatisfy({ states[$0]?.phase == .ready }) { return String(appLoc: "已就绪") }
        if downloadService.isAnyDownloading { return String(appLoc: "下载中…") }
        let readyCount = ModelKind.userFacing.filter { states[$0]?.phase == .ready }.count
        return readyCount == 0 ? String(appLoc: "未下载") : String(appLoc: "\(readyCount)/\(ModelKind.userFacing.count) 就绪")
    }

    private var inferenceEngineCard: some View {
        NavigationLink {
            InferenceSettingsView()
        } label: {
            settingsCard(title: String(appLoc: "推理引擎"), detail: engineDetail, icon: "cpu.fill")
        }
        .buttonStyle(.plain)
    }

    private var engineDetail: String {
        switch InferenceEngine.current {
        case .mnn: return InferenceEngine.cpuSupportsSME2 ? "MNN · SME2" : "MNN · CPU"
        case .mlx: return "MLX · GPU"
        }
    }

    private var languageCard: some View {
        NavigationLink {
            LanguageSettingsView()
        } label: {
            settingsCard(title: String(appLoc: "语言"),
                         detail: lang.current.displayName,
                         icon: "character.bubble")
        }
        .buttonStyle(.plain)
    }

    private var fontScaleCard: some View {
        NavigationLink {
            FontSettingsView()
        } label: {
            settingsCard(title: String(appLoc: "字体大小"),
                         detail: fontScale.scale.label,
                         icon: "textformat.size")
        }
        .buttonStyle(.plain)
    }

    // MARK: - Face ID 启动锁(可交互 Toggle 卡)

    private var faceIDCard: some View {
        HStack(spacing: 12) {
            ZStack {
                Circle().fill(lockEnabled ? Tj.Palette.amber.opacity(0.25) : Tj.Palette.sand2)
                Image(systemName: "faceid")
                    .font(.tjScaled( 18))
                    .foregroundStyle(lockEnabled ? Tj.Palette.ink : Tj.Palette.text2)
            }
            .frame(width: 44, height: 44)

            VStack(alignment: .leading, spacing: 2) {
                Text("Face ID 启动锁")
                    .font(.tjScaled( 15, weight: .medium))
                    .foregroundStyle(Tj.Palette.text)
                Text(faceIDLine)
                    .font(.tjScaled( 12))
                    .foregroundStyle(Tj.Palette.text3)
            }
            Spacer()
            Toggle("", isOn: faceIDBinding)
                .labelsHidden()
                .disabled(!appLock.biometryAvailable)
        }
        .padding(14)
        .tjCard()
    }

    private var faceIDLine: String {
        if !appLock.biometryAvailable { return String(appLoc: "本设备未设置 Face ID 或密码") }
        return lockEnabled ? String(appLoc: "已开启 · \(appLock.biometryLabel)") : String(appLoc: "关闭")
    }

    /// 打开 → 先认证一次,成功才置 enabled(失败则开关弹回);关闭 → 直接关。
    private var faceIDBinding: Binding<Bool> {
        Binding(
            get: { lockEnabled },
            set: { newValue in
                if newValue {
                    Task { await appLock.enableWithAuth() }
                } else {
                    appLock.disable()
                }
            }
        )
    }

    private func settingsCard(title: String, detail: String, icon: String) -> some View {
        HStack(spacing: 12) {
            ZStack {
                Circle().fill(Tj.Palette.sand2)
                Image(systemName: icon)
                    .font(.tjScaled( 18))
                    .foregroundStyle(Tj.Palette.text2)
            }
            .frame(width: 44, height: 44)

            Text(title)
                .font(.tjScaled( 15, weight: .medium))
                .foregroundStyle(Tj.Palette.text)
            Spacer()
            Text(detail)
                .font(.tjScaled( 12))
                .foregroundStyle(Tj.Palette.text3)
            Image(systemName: "chevron.right")
                .font(.tjScaled( 13, weight: .medium))
                .foregroundStyle(Tj.Palette.text3)
        }
        .padding(14)
        .tjCard()
    }

    private var profileLine: String {
        guard let p = profile, p.hasAnyBasics else {
            return String(appLoc: "点这里完善你的资料")
        }
        return p.summaryLine
    }
}

#Preview {
    MeView()
        .modelContainer(for: [
            UserProfile.self, Indicator.self, Report.self, DiaryEntry.self,
            Asset.self, ChatTurn.self, Symptom.self, MetricReminder.self,
            CustomMonitorMetric.self, Medication.self,
        ], inMemory: true)
}
