use std::sync::Arc;
use axum::{
    extract::{Path, State},
    response::Html,
};
use crate::state::AppState;
/// GET /user/{username} - Serve h-card profile page
pub async fn profile_handler(
    State(state): State<Arc<AppState>>,
    Path(username): Path<String>,
) -> Html<String> {
    // Look up user in AppState
    let user = match state.users.get(&username) {
        Some(u) => u,
        None => return Html::from(format!("<p>User '{}' not found</p>", username)),
    };
    
    // Build h-card HTML with microformats2
    let html = format!(
        r#"<html>
<head>
    <title>{name}'s Profile</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
</head>
<body>
<main class="container">
<article class="h-card">
    <h1 class="p-name">{name}</h1>
    {photo_html}
    {email_html}
    {url_html}
    <p>
        <a class="u-url u-uid" href="{me}">Profile URL</a>
    </p>
</article>
</main>
</body>
</html>"#,
        name = user.profile.name,
        photo_html = user.profile.photo.as_ref()
            .map(|p| format!(r#"<img class="u-photo" src="{}" alt="{}" style="max-width: 200px;">"#, p, user.profile.name))
            .unwrap_or_default(),
        email_html = user.profile.email.as_ref()
            .map(|e| format!(r#"<p><a class="u-email" href="mailto:{}">{}</a></p>"#, e, e))
            .unwrap_or_default(),
        url_html = user.profile.url.as_ref()
            .map(|u| format!(r#"<p><a class="u-url" href="{}">{}</a></p>"#, u, u))
            .unwrap_or_default(),
        me = user.profile.me,
    );
    
    Html(html)
}