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_episode::{
TvShowEpisodeDocument, TvShowEpisodeInclude, TvShowEpisodeRelation,
};
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(episode_id): Path<u64>,
QsQuery(params): QsQuery<FindQueryParams<TvShowEpisodeInclude>>,
) -> Result<Json<ApiResource<TvShowEpisodeDocument, TvShowEpisodeRelation>>, ApiErrorResponse>
where
S: crate::server::prelude::ServerState,
{
let language = entertainarr_domain::language::Language::En;
let response = state
.tvshow_service()
.find_tvshow_episode_by_id(Some(user_id), episode_id, language)
.await
.map_err(|err| {
tracing::error!(error = ?err, "unable to get tvshow episode");
ApiErrorResponse::internal()
})?;
let response = response.ok_or_else(|| ApiErrorResponse::not_found("episode not found"))?;
Ok(Json(ApiResource::from_response(response, params.include)))
}