/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.core.web

import android.content.ActivityNotFoundException
import android.content.Context
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent

/**
 * Opens the website's own pages in a Chrome Custom Tab (PLAN.md §4.2):
 * registration, invite acceptance, forgot/reset password, and SSO all stay
 * website-handled, so the app hands off rather than rebuilding those flows. The
 * user completes them in the browser and returns to sign in natively (§4.1).
 */
object WebHandoff {

    /**
     * Launch [url] in a Custom Tab. Returns false if no browser could handle it
     * (extremely rare on Android) so the caller can surface a fallback.
     */
    fun open(context: Context, url: String): Boolean = try {
        CustomTabsIntent.Builder()
            .setShowTitle(true)
            .build()
            .launchUrl(context, Uri.parse(url))
        true
    } catch (_: ActivityNotFoundException) {
        false
    }
}
