use axum::{extract::Path, response::Html}; use crate::services::ApiService; use crate::layout::layout; use chrono::NaiveDate; pub async fn bulletins_handler() -> Html { let api_service = ApiService::new(); match api_service.get_bulletins().await { Ok(bulletins) => { let content = format!(r#"

Church Bulletins

Download our weekly bulletins to stay informed about church activities and worship services

{}
{}
"#, // Bulletins grid HTML bulletins.iter().enumerate().map(|(index, bulletin)| { let formatted_date = if let Ok(parsed_date) = NaiveDate::parse_from_str(&bulletin.date, "%Y-%m-%d") { parsed_date.format("%A, %B %d, %Y").to_string() } else { bulletin.date.clone() }; format!(r#"

{}

{}

{}
View Details
"#, bulletin.id, (index % 3) + 1, bulletin.title, formatted_date, // Scripture reading preview if let Some(ref scripture) = bulletin.scripture_reading { if !scripture.is_empty() { let preview = if scripture.len() > 150 { format!("{}...", &scripture[..150]) } else { scripture.clone() }; format!(r#"
Scripture Reading:
{}
"#, preview) } else { String::new() } } else { String::new() } ) }).collect::>().join(""), // No bulletins message if bulletins.is_empty() { r#"

No Bulletins Available

No bulletins available at this time. Please check back later.

"# } else { "" } ); Html(layout(&content, "Bulletins")) }, Err(_) => { let content = r#"

Bulletins

Unable to load bulletins. Please try again later.

"#; Html(layout(content, "Bulletins")) } } } pub async fn bulletin_detail_handler(Path(id): Path) -> Html { let api_service = ApiService::new(); match api_service.get_bulletin(&id).await { Ok(Some(bulletin)) => { let formatted_date = if let Ok(parsed_date) = NaiveDate::parse_from_str(&bulletin.date, "%Y-%m-%d") { parsed_date.format("%A, %B %d, %Y").to_string() } else { bulletin.date.clone() }; let content = format!(r#"
{} {} {} {} "#, bulletin.title, formatted_date, // Scripture Reading Section if let Some(ref scripture) = bulletin.scripture_reading { if !scripture.is_empty() { format!(r#"

Scripture Reading

{}
"#, scripture) } else { String::new() } } else { String::new() }, // Service Programs Section if bulletin.sabbath_school.is_some() || bulletin.divine_worship.is_some() { let has_both = bulletin.sabbath_school.is_some() && bulletin.divine_worship.is_some(); format!(r#"

Service Programs

Order of service for worship and fellowship

{}
"#, if has_both { // Both programs - adaptive grid format!(r#"
{} {}
"#, // Sabbath School - smaller card if let Some(ref ss) = bulletin.sabbath_school { format!(r#"

Sabbath School

{}
"#, ss) } else { String::new() }, // Divine Worship - larger card if let Some(ref dw) = bulletin.divine_worship { format!(r#"

Divine Worship

{}
"#, dw) } else { String::new() } ) } else { // Single program or responsive fallback format!(r#"
{} {}
"#, if let Some(ref ss) = bulletin.sabbath_school { format!(r#"

Sabbath School Program

{}
"#, ss) } else { String::new() }, if let Some(ref dw) = bulletin.divine_worship { format!(r#"

Divine Worship Program

{}
"#, dw) } else { String::new() } ) }) } else { String::new() }, // Sunset Information Section if let Some(ref sunset) = bulletin.sunset { if !sunset.is_empty() { format!(r#"

Sabbath Information

{}

"#, sunset) } else { String::new() } } else { String::new() }, // PDF Download Section if let Some(ref pdf_path) = bulletin.pdf_path { if !pdf_path.is_empty() { format!(r#"

Download Full Bulletin

Get the complete bulletin with all details and information.

Download PDF
"#, pdf_path) } else { String::new() } } else { String::new() } ); Html(layout(&content, &bulletin.title)) }, Ok(None) => { let content = r#"

Bulletin Not Found

The requested bulletin could not be found.

← Back to Bulletins
"#; Html(layout(content, "Bulletin Not Found")) }, Err(_) => { let content = r#"

Error

Unable to load bulletin. Please try again later.

← Back to Bulletins
"#; Html(layout(content, "Error")) } } }