import SwiftUI

enum RecordKind: String, Identifiable, CaseIterable {
    case quick, indicator, healthExport, archive, diary, symptom, reminder
    var id: String { rawValue }

    /// RecordSheet 列表的展示顺序(从上到下)。与 enum 声明序解耦,改顺序只动这里。
    /// 注:`.quick`(指标速记)已并入 `.indicator`(记录指标)内的「拍照识别」,不再单列。
    static let displayOrder: [RecordKind] = [.diary, .reminder, .symptom, .indicator, .healthExport, .archive]

    var title: String {
        switch self {
        case .quick:     return String(appLoc: "指标速记")
        case .indicator: return String(appLoc: "记录指标")
        case .healthExport: return String(appLoc: "身体档案")
        case .archive:   return String(appLoc: "体检报告归档")
        case .diary:     return String(appLoc: "健康日记")
        case .symptom:   return String(appLoc: "记录症状")
        case .reminder:  return String(appLoc: "开启一个提醒")
        }
    }
    var subtitle: String {
        switch self {
        case .quick:     return String(appLoc: "拍一张化验单,VL 自动识别")
        case .indicator: return String(appLoc: "手动填写,或拍照自动识别")
        case .healthExport: return String(appLoc: "多轮问答后生成给医生看的整理报告")
        case .archive:   return String(appLoc: "完整保存整份报告（可多页）")
        case .diary:     return String(appLoc: "记录身体状态、用药、感受 · 可让 AI 辅助")
        case .symptom:   return String(appLoc: "开始一个持续症状,结束时再点结束")
        case .reminder:  return String(appLoc: "管理用药、复查、监测的周期提醒")
        }
    }
    var icon: String {
        switch self {
        case .quick:     return "camera.fill"
        case .indicator: return "number.square.fill"
        case .healthExport: return "doc.text.below.ecg"
        case .archive:   return "doc.fill"
        case .diary:     return "heart.text.square"
        case .symptom:   return "waveform.path.ecg"
        case .reminder:  return "bell.badge"
        }
    }
    var accent: Color {
        switch self {
        case .quick:     return Tj.Palette.brick
        case .indicator: return Tj.Palette.brick
        case .healthExport: return Tj.Palette.ink
        case .archive:   return Tj.Palette.ink
        case .diary:     return Tj.Palette.leaf
        case .symptom:   return Tj.Palette.amber
        case .reminder:  return Tj.Palette.leaf
        }
    }
}

struct RecordSheet: View {
    var onPick: (RecordKind) -> Void

    var body: some View {
        VStack(spacing: 0) {
            Capsule()
                .fill(Tj.Palette.line)
                .frame(width: 40, height: 4)
                .padding(.top, 10)
                .padding(.bottom, 16)

            HStack {
                Text("记录什么？")
                    .font(.tjH2())
                    .foregroundStyle(Tj.Palette.text)
                Spacer()
                Text("本地处理 · 永不上传")
                    .font(.tjScaled( 12))
                    .foregroundStyle(Tj.Palette.text3)
            }
            .padding(.bottom, 14)

            // ScrollView 包裹:6 个入口在小屏固定 detent 下可能溢出,滚动确保都能触达。
            ScrollView {
              VStack(spacing: 10) {
                ForEach(RecordKind.displayOrder) { kind in
                    Button {
                        onPick(kind)
                    } label: {
                        HStack(spacing: 14) {
                            ZStack {
                                RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
                                    .fill(kind.accent)
                                Image(systemName: kind.icon)
                                    .font(.tjScaled( 18, weight: .medium))
                                    .foregroundStyle(Tj.Palette.paper)
                            }
                            .frame(width: 44, height: 44)

                            VStack(alignment: .leading, spacing: 2) {
                                Text(kind.title)
                                    .font(.tjScaled( 15, weight: .semibold))
                                    .foregroundStyle(Tj.Palette.text)
                                Text(kind.subtitle)
                                    .font(.tjScaled( 12))
                                    .foregroundStyle(Tj.Palette.text3)
                            }
                            Spacer()
                            Image(systemName: "chevron.right")
                                .font(.tjScaled( 14, weight: .medium))
                                .foregroundStyle(Tj.Palette.text3)
                        }
                        .padding(16)
                        .tjCard()
                    }
                    .buttonStyle(.plain)
                }
              }
              .padding(.bottom, 22)
            }
            .scrollIndicators(.hidden)
        }
        .padding(.horizontal, 18)
        .background(
            Tj.Palette.sand
                .clipShape(RoundedRectangle(cornerRadius: Tj.Radius.xl, style: .continuous))
                .ignoresSafeArea(edges: .bottom)
        )
        .presentationDetents([.fraction(0.8)])
        .presentationDragIndicator(.hidden)
        .presentationBackground(Tj.Palette.sand)
        .presentationCornerRadius(Tj.Radius.xl)
    }
}

#Preview("RecordSheet · 直接渲染") {
    RecordSheet { kind in print("picked: \(kind)") }
        .frame(width: 390, height: 560)
        .background(Tj.Palette.sand)
}

#Preview("RecordSheet · sheet 模式") {
    PreviewContainer()
}

private struct PreviewContainer: View {
    @State private var show = true
    var body: some View {
        Text("点这里再开一次")
            .onTapGesture { show = true }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Tj.Palette.sand.ignoresSafeArea())
            .sheet(isPresented: $show) {
                RecordSheet { kind in print("picked: \(kind)"); show = false }
            }
    }
}
