use axum::http::HeaderMap;
use result_extended::ResultExt;
use serde::{Deserialize, Serialize};

use crate::{
    core::Core,
    database::GameId,
    types::{AppError, FatalError},
};

use super::auth_required;

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct CreateGameRequest {
    pub game_type: String,
    pub game_name: String,
}

pub async fn create_game(
    core: Core,
    headers: HeaderMap,
    req: CreateGameRequest,
) -> ResultExt<GameId, AppError, FatalError> {
    auth_required(core.clone(), headers, |user| async move {
        core.create_game(&user.id, &req.game_type, &req.game_name)
            .await
    })
    .await
}