use dioxus::prelude::*;

use crate::i18n::{t, Locale};
use crate::Route;

/// Login redirect component.
///
/// Redirects the user to the external OAuth authentication endpoint.
/// If no `redirect_url` is provided, defaults to `/dashboard`.
///
/// # Arguments
///
/// * `redirect_url` - URL to redirect to after successful authentication
#[component]
pub fn Login(redirect_url: String) -> Element {
    let navigator = use_navigator();
    let locale = use_context::<Signal<Locale>>();
    let l = *locale.read();

    use_effect(move || {
        // Default to /dashboard when redirect_url is empty.
        let destination = if redirect_url.is_empty() {
            "/dashboard".to_string()
        } else {
            redirect_url.clone()
        };
        let target = format!("/auth?redirect_url={destination}");
        navigator.push(NavigationTarget::<Route>::External(target));
    });

    rsx!(
        div { class: "text-center p-6", "{t(l, \"auth.redirecting_secure\")}" }
    )
}
