import SwiftUI

/// The step between asking for a scooter and getting one: read the policy, then agree.
///
/// Shown before anything is reserved or unlocked, and only while something is outstanding, so a
/// rider who has already agreed never sees it again until a new version is published.
///
/// The documents open in the browser rather than rendering here on purpose: the wording is legal
/// text that changes on its own schedule, and shipping a copy inside the app would mean the
/// version a rider reads is the version we last released, not the one they are agreeing to.
struct ConsentView: View {
    let documents: [LegalDocument]
    let submitting: Bool
    let failed: Bool
    let onAccept: () -> Void
    let onDecline: () -> Void

    var body: some View {
        ScrollView {
            VStack(spacing: 14) {
                IconChip(glyph: "🛡", tone: .brand, size: 56)

                Text("consent_title".localized)
                    .font(.beebTitle)
                    .multilineTextAlignment(.center)

                Text("consent_body".localized)
                    .font(.beebBody)
                    .foregroundStyle(Color.muted)
                    .multilineTextAlignment(.center)

                ForEach(documents) { document in
                    Link(destination: URL(string: document.url) ?? URL(string: "https://beebbeeb.sa")!) {
                        HStack(spacing: 12) {
                            VStack(alignment: .leading, spacing: 2) {
                                Text(label(for: document.key))
                                    .font(.beebBody)
                                    .foregroundStyle(Color.appForeground)
                                // The version is part of what they are agreeing to, so it is on
                                // screen rather than only in the record we keep.
                                Text(String(format: "consent_version".localized, document.version))
                                    .font(.beebCaption)
                                    .foregroundStyle(Color.muted)
                            }
                            Spacer()
                            Image(systemName: "arrow.up.right.square")
                                .foregroundStyle(Color.muted)
                        }
                        .padding(16)
                        .beebCard()
                    }
                }

                if failed {
                    Text("consent_failed".localized)
                        .font(.beebBody)
                        .foregroundStyle(Color.danger)
                        .multilineTextAlignment(.center)
                }

                BrandButton(
                    title: (submitting ? "consent_saving" : "consent_accept").localized,
                    enabled: !submitting,
                    action: onAccept
                )
                SecondaryButton(title: "cancel".localized, action: onDecline)
            }
            .padding(20)
        }
        .background(Color.appBackground)
    }

    /// Known documents get proper names; anything new falls back rather than showing a raw key.
    private func label(for key: String) -> String {
        switch key {
        case "privacy": return "consent_doc_privacy".localized
        case "terms": return "consent_doc_terms".localized
        default: return "consent_doc_other".localized
        }
    }
}
