//! Drug Interaction Handlers (Phase 2.8) use axum::{ extract::{Extension, State}, http::StatusCode, Json, }; use serde::{Deserialize, Serialize}; use crate::{ auth::jwt::Claims, config::AppState, services::openfda_service::{DrugInteraction, InteractionSeverity}, }; #[derive(Debug, Deserialize)] pub struct CheckInteractionRequest { pub medications: Vec, } #[derive(Debug, Serialize)] pub struct InteractionResponse { pub interactions: Vec, pub has_severe: bool, pub disclaimer: String, } /// Check interactions between medications pub async fn check_interactions( _claims: Extension, State(state): State, Json(request): Json, ) -> Result, StatusCode> { if request.medications.len() < 2 { return Err(StatusCode::BAD_REQUEST); } let interaction_service = state .interaction_service .as_ref() .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; match interaction_service .check_eu_medications(&request.medications) .await { Ok(interactions) => { let has_severe = interactions .iter() .any(|i| matches!(i.severity, InteractionSeverity::Severe)); Ok(Json(InteractionResponse { interactions, has_severe, disclaimer: "This information is advisory only. Consult with a physician for detailed information about drug interactions.".to_string(), })) } Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR), } } #[derive(Debug, Deserialize)] pub struct CheckNewMedicationRequest { pub new_medication: String, pub existing_medications: Vec, } #[derive(Debug, Serialize)] pub struct NewMedicationCheckResult { pub interactions: Vec, pub has_severe: bool, pub disclaimer: String, } /// Check if a new medication has interactions with existing medications pub async fn check_new_medication( _claims: Extension, State(state): State, Json(request): Json, ) -> Result, StatusCode> { let interaction_service = state .interaction_service .as_ref() .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; match interaction_service .check_new_medication(&request.new_medication, &request.existing_medications) .await { Ok(result) => Ok(Json(InteractionResponse { interactions: result.interactions, has_severe: result.has_severe, disclaimer: result.disclaimer, })), Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR), } }