agent-supplements-rec 0.1.0

Curated supplement recommendation engine for longevity biomarker optimization
use crate::catalog;
use crate::types::{CatalogFile, Product};

/// Filter products based on optional category, brand, and biomarker filters.
pub fn filter_products<'a>(
    catalog_file: &'a CatalogFile,
    category: Option<&str>,
    brand: Option<&str>,
    biomarker: Option<&str>,
    limit: usize,
) -> Vec<&'a Product> {
    let mut results: Vec<&Product> = if let Some(cat) = category {
        catalog::products_by_category(catalog_file, cat)
    } else if let Some(br) = brand {
        catalog::products_by_brand(catalog_file, br)
    } else if let Some(bio) = biomarker {
        catalog::products_for_biomarker(catalog_file, bio)
    } else {
        catalog_file.product.iter().collect()
    };

    // Apply secondary filters when a primary filter was used
    if let Some(cat) = category {
        let cat_lower = cat.to_lowercase();
        results.retain(|p| p.category.to_lowercase() == cat_lower);
    }
    if let Some(br) = brand {
        let br_lower = br.to_lowercase();
        results.retain(|p| p.brand_id.to_lowercase() == br_lower);
    }
    if let Some(bio) = biomarker {
        let bio_lower = bio.to_lowercase();
        results.retain(|p| {
            p.targets
                .iter()
                .any(|t| t.biomarker.to_lowercase() == bio_lower)
        });
    }

    results.truncate(limit);
    results
}