use axum::{
    http::{StatusCode, header},
    response::Response,
};
use log;
use serde::Serialize;

pub struct ResponseBuilder;

impl ResponseBuilder {
    pub fn json<T: Serialize>(data: T) -> Result<Response, StatusCode> {
        let json = serde_json::to_vec(&data).map_err(|e| {
            log::warn!("Failed to serialize response: {e}");
            StatusCode::INTERNAL_SERVER_ERROR
        })?;

        Response::builder()
            .header(header::CONTENT_TYPE, "application/json")
            .header(header::CONTENT_LENGTH, json.len().to_string())
            .body(axum::body::Body::from(json))
            .map_err(|e| {
                log::warn!("Failed to build response: {e}");
                StatusCode::INTERNAL_SERVER_ERROR
            })
    }

    pub fn binary(content: &[u8], mime_type: &str) -> Result<Response, StatusCode> {
        Response::builder()
            .header(header::CONTENT_TYPE, mime_type)
            .header(header::CONTENT_LENGTH, content.len().to_string())
            .body(axum::body::Body::from(content.to_vec()))
            .map_err(|e| {
                log::warn!("Failed to build response: {e}");
                StatusCode::INTERNAL_SERVER_ERROR
            })
    }
}
