use axum::{extract::Extension, http::StatusCode, Json};

use crate::{app_state::{AppState, SessionSnapshot, SessionStartRequest}, error::AppError};

pub async fn start(
    Extension(state): Extension<AppState>,
    Json(payload): Json<SessionStartRequest>,
) -> Result<(StatusCode, Json<SessionSnapshot>), AppError> {
    let session = state.start_session(payload).await;
    Ok((StatusCode::CREATED, Json(session)))
}

pub async fn end(Extension(state): Extension<AppState>) -> Result<Json<SessionSnapshot>, AppError> {
    let session = state.end_session().await?;
    Ok(Json(session))
}

pub async fn me(Extension(state): Extension<AppState>) -> Result<Json<SessionSnapshot>, AppError> {
    let Some(session) = state.current_session().await else {
        return Err(AppError::not_found("No active session"));
    };
    Ok(Json(session))
}
