mod components;
mod pages;

use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use pages::{
    auth::{
        forgot::ForgotPage, login::LoginPage, logout::LogoutPage, register::RegisterPage,
        reset::ResetPage,
    },
    dashboard::DashboardPage,
    error::{AppError, ErrorTemplate},
};

#[component]
pub fn App() -> impl IntoView {
    provide_meta_context();

    let trigger_user = create_rw_signal(true);

    let user = create_resource(trigger_user, move |_| async move {
        super::check_user().await.unwrap()
    });

    view! {
        // https://fontawesome.com/v5/search
        <Stylesheet id="font-awesome" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"/>
        <Stylesheet id="avam" href="/pkg/avam.css"/>

        <Meta name="viewport" content="width=device-width, initial-scale=1"/>

        <Link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"/>
        <Link rel="icon" type_="image/png" sizes="32x32" href="/favicon-32x32.png"/>
        <Link rel="icon" type_="image/png" sizes="16x16" href="/favicon-16x16.png"/>
        <Link rel="manifest" href="/site.webmanifest"/>

        <Title text={ crate::PROJECT_NAME }/>

        <Router fallback=|| {
            let mut outside_errors = Errors::default();
            outside_errors.insert_with_default_key(AppError::NotFound);
            view! {
                <ErrorTemplate outside_errors/>
            }
            .into_view()
        }>
            <main class="h-screen overflow-auto dark:base-100 dark:text-white">
                <Routes>
                    <Route path="/auth" view=move || {
                        view! {
                            <Suspense>
                                <Show when=move || user().is_some_and(|u| u.is_some())>
                                    <Redirect path="/" />
                                </Show>
                            </Suspense>
                            <Outlet />
                        }
                    }>
                        <Route path="login" view=move || view! { <LoginPage user_signal=trigger_user /> } />
                        <Route path="register" view=RegisterPage />
                        <Route path="forgot" view=ForgotPage />
                        <Route path="reset/:token" view=ResetPage />
                    </Route> // auth

                    <Route path="" view=move || {
                        view! {
                            <Suspense>
                                <Show when=move || user().is_some_and(|u| u.is_none())>
                                    <Redirect path="/auth/login" />
                                </Show>
                            </Suspense>
                            <Outlet />
                        }
                    }>
                        <Route path="" view=move || view! {
                            <Suspense>
                                <Show when=move || user().is_some_and(|u| u.is_some())>
                                    <DashboardPage user={ user().unwrap().unwrap() } />
                                </Show>
                            </Suspense>
                        } />
                    </Route> // dashboard
                    <Route path="/auth/logout" view=move || view! { <LogoutPage user_signal=trigger_user /> } />
                </Routes>
            </main>
        </Router>
    }
}
