import SwiftUI

struct B3MetaView: View {
    var onAnalyze: () -> Void
    var onBack: () -> Void

    @State private var selectedType = 0
    private let types = [
        String(appLoc: "体检报告"),
        String(appLoc: "化验单"),
        String(appLoc: "影像报告"),
        String(appLoc: "处方"),
        String(appLoc: "其他"),
    ]

    var body: some View {
        VStack(spacing: 0) {
            header
            ScrollView(showsIndicators: false) {
                VStack(alignment: .leading, spacing: 0) {
                    Text("报告类型")
                        .font(.system(size: 11))
                        .tracking(0.5)
                        .foregroundStyle(Tj.Palette.text3)
                        .padding(.bottom, 8)

                    typeChips.padding(.bottom, 20)

                    FormRow(label: "报告日期", value: "2026 / 05 / 25", subtle: false)
                    FormRow(label: "出具机构", value: "协和医院体检中心", subtle: true)
                    FormRow(label: "备注",     value: "春季年度体检",   subtle: true)

                    Text("已拍页面（3 页）")
                        .font(.system(size: 11))
                        .tracking(0.5)
                        .foregroundStyle(Tj.Palette.text3)
                        .padding(.top, 20)
                        .padding(.bottom, 10)

                    HStack(spacing: 10) {
                        ForEach(1...3, id: \.self) { n in
                            PageCard(index: n)
                        }
                    }
                }
                .padding(.horizontal, 18)
                .padding(.bottom, 18)
            }

            VStack(spacing: 8) {
                Button(action: onAnalyze) {
                    Text("开始 AI 解读").frame(maxWidth: .infinity)
                }
                .buttonStyle(TjPrimaryButton())

                Text("预计耗时 5–8 秒 · 端侧 SME2 加速")
                    .font(.system(size: 11))
                    .foregroundStyle(Tj.Palette.text3)
            }
            .padding(.horizontal, 18)
            .padding(.bottom, 14)
        }
        .background(Tj.Palette.sand.ignoresSafeArea())
    }

    private var header: some View {
        HStack(spacing: 6) {
            Button(action: onBack) {
                Image(systemName: "chevron.left")
                    .font(.system(size: 18, weight: .semibold))
                    .foregroundStyle(Tj.Palette.text)
                    .frame(width: 36, height: 36)
            }
            Text("归档信息")
                .font(.system(size: 15, weight: .semibold))
                .foregroundStyle(Tj.Palette.text)
            Spacer()
        }
        .padding(.horizontal, 12)
        .padding(.top, 4)
        .padding(.bottom, 8)
    }

    private var typeChips: some View {
        let columns = [GridItem(.adaptive(minimum: 60, maximum: 200), spacing: 8)]
        return LazyVGrid(columns: columns, alignment: .leading, spacing: 8) {
            ForEach(Array(types.enumerated()), id: \.offset) { idx, t in
                Button { selectedType = idx } label: {
                    Text(t)
                        .font(.system(size: 12, weight: .medium))
                        .foregroundStyle(idx == selectedType ? Tj.Palette.paper : Tj.Palette.text2)
                        .padding(.horizontal, 12)
                        .padding(.vertical, 6)
                        .background(
                            Capsule().fill(idx == selectedType ? Tj.Palette.ink : Tj.Palette.sand2)
                        )
                }
                .buttonStyle(.plain)
            }
        }
    }
}

private struct FormRow: View {
    let label: String
    let value: String
    let subtle: Bool

    var body: some View {
        HStack {
            Text(label).font(.system(size: 13)).foregroundStyle(Tj.Palette.text2)
            Spacer()
            HStack(spacing: 6) {
                Text(value)
                    .font(.system(size: 13))
                    .foregroundStyle(subtle ? Tj.Palette.text3 : Tj.Palette.text)
                Image(systemName: "chevron.right")
                    .font(.system(size: 11, weight: .medium))
                    .foregroundStyle(Tj.Palette.text3)
            }
        }
        .padding(.vertical, 12)
        .overlay(alignment: .top) {
            Rectangle().fill(Tj.Palette.lineSoft).frame(height: 1)
        }
    }
}

private struct PageCard: View {
    let index: Int

    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
                .fill(Tj.Palette.paper)
                .shadow(color: Color(red: 0.196, green: 0.157, blue: 0.098).opacity(0.06),
                        radius: 2, x: 0, y: 1)
            TjPlaceholder(label: "p.\(index)", radius: 4)
                .padding(6)
        }
        .aspectRatio(0.72, contentMode: .fit)
    }
}
