//! Explicit server-function registration for the website binary.
//!
//! Auto-registration via `inventory::submit!` is dropped by the linker in some
//! release configurations (LTO, `--gc-sections`, container builds), which
//! makes `handle_server_fns` return `400 "Could not find a server function at
//! the route /api/<fn><hash>"` even though the function exists in the
//! dependency graph. Registering each type explicitly at startup bypasses the
//! linker's dead-code analysis and guarantees every server function is
//! reachable.

#![cfg(not(target_arch = "wasm32"))]

use leptos::server_fn::axum::{register_explicit, server_fn_paths};

use rustelo_components_leptos::activities::server_fns::{
    CheckActivityAccess, GetQuestionnaireDefinition, RecordActivityCompletion,
    SubmitInternalQuestionnaire,
};
use rustelo_components_leptos::auth::server_fns::{
    AcceptGdpr, AddBookmark, CreateNote, CreateServiceToken, DeleteAccount, DeleteMessage,
    DeleteNote, GetBookmarks, GetMessages, GetNotes, GetResources, GetSessionUser, IsBookmarked,
    ListServiceTokens, Logout, LogoutAll, MarkMessageRead, RefreshToken, RemoveBookmark,
    RequestOtp, RevokeServiceToken, UpdateDisplayName, UpdateNote, VerifyOtp,
};
use rustelo_components_leptos::contact::server_fns::SendContactForm;
use rustelo_pages_leptos::nav_log::server_fns::TrackPageView;

/// Register every server function the website exposes.
///
/// Must run on the main thread before the Axum router starts; the registry it
/// writes to is consulted by `leptos_axum::handle_server_fns` on every
/// `POST /api/<fn><hash>` request.
pub fn register_all() {
    // rustelo_pages_leptos
    register_explicit::<TrackPageView>();

    // rustelo_components_leptos::contact
    register_explicit::<SendContactForm>();

    // rustelo_components_leptos::auth
    register_explicit::<RequestOtp>();
    register_explicit::<VerifyOtp>();
    register_explicit::<AcceptGdpr>();
    register_explicit::<Logout>();
    register_explicit::<LogoutAll>();
    register_explicit::<RefreshToken>();
    register_explicit::<GetSessionUser>();
    register_explicit::<DeleteAccount>();
    register_explicit::<UpdateDisplayName>();
    register_explicit::<GetBookmarks>();
    register_explicit::<AddBookmark>();
    register_explicit::<RemoveBookmark>();
    register_explicit::<IsBookmarked>();
    register_explicit::<GetMessages>();
    register_explicit::<MarkMessageRead>();
    register_explicit::<DeleteMessage>();
    register_explicit::<GetNotes>();
    register_explicit::<CreateNote>();
    register_explicit::<UpdateNote>();
    register_explicit::<DeleteNote>();
    register_explicit::<GetResources>();
    register_explicit::<ListServiceTokens>();
    register_explicit::<CreateServiceToken>();
    register_explicit::<RevokeServiceToken>();

    // rustelo_components_leptos::activities
    register_explicit::<CheckActivityAccess>();
    register_explicit::<RecordActivityCompletion>();
    register_explicit::<GetQuestionnaireDefinition>();
    register_explicit::<SubmitInternalQuestionnaire>();

    let paths: Vec<String> = server_fn_paths()
        .map(|(p, m)| format!("{m} {p}"))
        .collect();
    tracing::info!(
        target: "server_fn_register",
        count = paths.len(),
        "explicit server-function registration completed"
    );
    for entry in &paths {
        tracing::info!(target: "server_fn_register", route = %entry, "registered");
    }
}
