use leptos::prelude::*;

/// Send a contact/work-request form via the registered email service.
///
/// `subject` and `message` are pre-composed by the caller.  The server-side
/// adapter validates, rate-limits, and dispatches via the configured provider
/// (SMTP, SendGrid, or Console in dev).
#[server(SendContactForm, "/api")]
pub async fn server_send_contact_form(
    name: String,
    email: String,
    subject: String,
    message: String,
) -> Result<(), ServerFnError> {
    #[cfg(feature = "ssr")]
    {
        use axum::Extension;
        use leptos_axum::extract;

        use crate::contact::facade::server::ContactFormExtension;

        let Extension(ContactFormExtension(svc)) = extract::<Extension<ContactFormExtension>>()
            .await
            .map_err(|_| {
                ServerFnError::new(
                    "Contact form service not configured — register ContactFormExtension at \
                     startup",
                )
            })?;

        svc.send(name, email, subject, message)
            .await
            .map_err(ServerFnError::new)?;
    }

    Ok(())
}
