use axum::{extract::{Extension, Path}, Json};
use uuid::Uuid;

use crate::{app_state::{AppState, EventManifestSnapshot, EventSnapshot}, error::AppError};

pub async fn next(Extension(state): Extension<AppState>) -> Result<Json<EventSnapshot>, AppError> {
    let Some(event) = state.next_event().await else {
        return Err(AppError::not_found("No event available"));
    };
    Ok(Json(event))
}

pub async fn show(
    Path(event_id): Path<Uuid>,
    Extension(state): Extension<AppState>,
) -> Result<Json<EventSnapshot>, AppError> {
    let Some(event) = state.event(event_id).await else {
        return Err(AppError::not_found("Event not found"));
    };
    Ok(Json(event))
}

pub async fn manifest(
    Path(event_id): Path<Uuid>,
    Extension(state): Extension<AppState>,
) -> Result<Json<EventManifestSnapshot>, AppError> {
    let Some(manifest) = state.event_manifest(event_id).await else {
        return Err(AppError::not_found("Event manifest not found"));
    };
    Ok(Json(manifest))
}
