use rocket::{
    fairing::{Fairing, Info, Kind},
    Request, Response,
};

use crate::metrics::REQUEST_COUNTER;

pub struct MetricProducer;

#[rocket::async_trait]
impl Fairing for MetricProducer {
    fn info(&self) -> Info {
        Info { name: "Metrics fairing", kind: Kind::Response }
    }

    async fn on_response<'r>(&self, req: &'r Request<'_>, resp: &mut Response<'r>) {
        if let Some(route) = req.route() {
            REQUEST_COUNTER
                .with_label_values(&[
                    req.method().as_str(),
                    &resp.status().code.to_string(),
                    &route.uri.to_string(),
                ])
                .inc();
        }
    }
}
