//! JWT Authentication middleware
//!
//! Validates Bearer tokens on protected routes.
//! This is a placeholder — implement your JWT validation logic here.
//!
//! Usage in main.rs:
//!     App::new()
//!         .service(my_api::routes::configure_with_auth)
//!         ...
//!
//! Or use as a request guard in routes.

use actix_web::{
    body::BoxBody,
    dev::{ServiceRequest, ServiceResponse},
    Error,
};
use std::future::{ready, Ready};

/// JWT auth middleware — validates Bearer tokens on protected routes.
/// Currently a pass-through placeholder; implement your JWT validation logic.
pub struct JwtAuth {
    secret: String,
}

impl JwtAuth {
    pub fn new(secret: String) -> Self {
        Self { secret }
    }

    /// Validate a Bearer token. Returns Ok(()) if valid, Err(status) if not.
    pub fn validate_token(&self, token: &str) -> Result<(), actix_web::http::StatusCode> {
        // TODO: Implement actual JWT validation using self.secret:
        //   let key = jsonwebtoken::DecodingKey::from_secret(self.secret.as_bytes());
        //   let token_data = jsonwebtoken::decode(token, &key, &Validation::default())?;
        //   Ok(())
        tracing::debug!("JWT validation placeholder called for token");
        Ok(())
    }
}

/// Check Authorization header for a request.
pub fn extract_bearer_token(req: &ServiceRequest) -> Option<String> {
    use actix_web::http::header::AUTHORIZATION;
    req.headers()
        .get(AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.strip_prefix("Bearer "))
        .map(|s| s.to_string())
}