use axum::{extract::State, Json}; use serde_json::{json, Value}; use crate::{ app::AppState, error::AppResult, models::season::{PROMOTION_PTS, RELEGATION_PTS, SEASON_LENGTH}, services::{club as club_svc, profile as profile_svc, season as season_svc}, }; pub async fn get_division(State(state): State) -> AppResult> { let profile = profile_svc::get_active_profile(&state.pool).await?; let _club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?; let season = season_svc::get_or_create(&state.pool, &profile.id).await?; Ok(Json(json!({ "division": season.division, "season_number": season.season_number, "season_points": season.season_points, "matches_played": season.matches_played, "matches_remaining": season.matches_remaining(), "season_length": SEASON_LENGTH, "promotion_pts": PROMOTION_PTS, "relegation_pts": RELEGATION_PTS, "pts_above_safe": season.pts_above_safe(), "promotion_achievable": season.promotion_achievable(), "can_be_relegated": season.can_be_relegated(), "record": { "wins": season.wins, "draws": season.draws, "losses": season.losses, }, "pts_for_promotion": season.pts_for_promotion(), "started_at": season.started_at, }))) } pub async fn get_division_history(State(state): State) -> AppResult> { let profile = profile_svc::get_active_profile(&state.pool).await?; let history = season_svc::get_history(&state.pool, &profile.id).await?; Ok(Json(json!({ "history": history, "total": history.len() }))) }