import SwiftUI
import SafariServices

/// Embeds Safari's full engine — including Apple's Private Access Token
/// plumbing — inside the app. WKWebView in third-party apps can't pass
/// Cloudflare Turnstile (the PAT issuance pipeline gates on browser-app
/// status); SFSafariViewController is the only system-provided in-app
/// browser that does. Cookies persist across launches and share Safari's
/// cookie jar, so Turnstile clearance survives.
///
/// We expose this view both from Settings → Tools (full-screen browse) and
/// from the Search tab (when the user wants multi-stop / where-can-I-go,
/// neither of which we replicate via FlightAware).
struct RouteExplorerBrowserView: UIViewControllerRepresentable {
    let url: URL

    init(url: URL = URL(string: "https://route-explorer.com/")!) {
        self.url = url
    }

    func makeUIViewController(context: Context) -> SFSafariViewController {
        let config = SFSafariViewController.Configuration()
        // Keep the URL bar visible — it doubles as a trust indicator that
        // we're really on route-explorer.com.
        config.barCollapsingEnabled = false
        config.entersReaderIfAvailable = false
        let vc = SFSafariViewController(url: url, configuration: config)
        vc.preferredControlTintColor = .systemBlue
        // Page sheet-style dismiss feels natural inside a navigation flow.
        vc.dismissButtonStyle = .done
        DiagnosticLogger.shared.log("REBR", "open", ["url": url.absoluteString])
        return vc
    }

    func updateUIViewController(_ vc: SFSafariViewController, context: Context) {}
}
