//! Derive macro for HttpResponse trait.
//!
//! Generates an `into_http_response` that serializes the struct to JSON via serde,
//! plus an `IntoResponse` impl so the type can be used directly as axum handler return.
//!
//! Sealed<T> fields are automatically handled by Sealed's Serialize impl —
//! no special field-level logic needed here.

use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;

/// Generate the HttpResponse + IntoResponse impl for a struct.
///
/// Requires the struct to also derive `serde::Serialize`.
/// Sealed<T> fields are transparently serialized via Sealed's Serialize impl.
pub fn derive_http_response(input: DeriveInput) -> TokenStream {
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    quote! {
        impl #impl_generics ::mad_hatter::http_response::HttpResponse for #name #ty_generics #where_clause {
            fn into_http_response(self) -> ::mad_hatter::axum::response::Response {
                ::mad_hatter::axum::response::IntoResponse::into_response(
                    ::mad_hatter::axum::Json(
                        ::mad_hatter::serde_json::to_value(&self)
                            .unwrap_or(::mad_hatter::serde_json::Value::Null)
                    )
                )
            }
        }

        impl #impl_generics ::mad_hatter::axum::response::IntoResponse for #name #ty_generics #where_clause {
            fn into_response(self) -> ::mad_hatter::axum::response::Response {
                ::mad_hatter::http_response::HttpResponse::into_http_response(self)
            }
        }
    }
}