//! Home page handlers

use axum::{
    extract::State,
    response::Html,
};
use sqlx::SqlitePool;

#[derive(Clone)]
pub struct AppState {
    pub pool: SqlitePool,
}

/// Serves the home page dashboard
pub async fn index_handler(State(state): State<AppState>) -> Html<String> {
    let player_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM players")
        .fetch_one(&state.pool)
        .await
        .unwrap_or(0);
    
    let match_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM matches")
        .fetch_one(&state.pool)
        .await
        .unwrap_or(0);
    
    let session_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM sessions")
        .fetch_one(&state.pool)
        .await
        .unwrap_or(0);
    
    // For now, return simple HTML. Will be replaced with Askama template in next iteration
    let html = format!(
        r#"<!DOCTYPE html><html><head><title>Pickleball ELO</title></head><body>
        <h1>🏓 Pickleball ELO Tracker</h1>
        <p>Matches: {}</p>
        <p>Players: {}</p>
        <p>Sessions: {}</p>
        </body></html>"#,
        match_count, player_count, session_count
    );
    
    Html(html)
}

/// Serves the about page
pub async fn about_handler() -> Html<String> {
    let html = r#"<!DOCTYPE html>
<html>
<head>
    <title>About - Pickleball ELO</title>
</head>
<body>
    <h1>❓ About</h1>
    <p>Pickleball ELO Rating System v3.0</p>
</body>
</html>"#;
    
    Html(html.to_string())
}
