use axum::Json;
use axum::extract::{Path, State};
use entertainarr_domain::tvshow::prelude::TvShowService;
use serde_qs::axum::QsQuery;
use crate::entity::ApiResource;
use crate::entity::prelude::FindQueryParams;
use crate::entity::tvshow_season::{
TvShowSeasonDocument, TvShowSeasonInclude, TvShowSeasonRelation,
};
use crate::server::extractor::user::CurrentUser;
use crate::server::handler::error::ApiErrorResponse;
use crate::server::handler::prelude::FromDomainResponse;
pub async fn handle<S>(
State(state): State<S>,
CurrentUser(user_id): CurrentUser,
Path(season_id): Path<u64>,
QsQuery(params): QsQuery<FindQueryParams<TvShowSeasonInclude>>,
) -> Result<Json<ApiResource<TvShowSeasonDocument, TvShowSeasonRelation>>, ApiErrorResponse>
where
S: crate::server::prelude::ServerState,
{
let language = entertainarr_domain::language::Language::En;
let response = state
.tvshow_service()
.find_tvshow_season_by_id(Some(user_id), season_id, language)
.await
.map_err(|err| {
tracing::error!(error = ?err, "unable to fetch tvshow season");
ApiErrorResponse::internal()
})?
.ok_or_else(|| ApiErrorResponse::not_found("tvshow season not found"))?;
Ok(Json(ApiResource::from_response(response, params.include)))
}