import { ApiError, request } from './client';
import type { components } from './generated/v1';

export type MovieDetail = components['schemas']['MovieDetailResponseModel'];
export type ShowDetail = components['schemas']['ShowDetailResponseModel'];
export type SeasonDetail = components['schemas']['SeasonDetailResponseModel'];
export type ArtistDetail = components['schemas']['ArtistDetailResponseModel'];
export type ActorDetail = components['schemas']['ActorResponseModel'];
export type MediaItemInfo = components['schemas']['MediaItemInfoResponseModel'];
export type MediaItemInfoStream = components['schemas']['MediaItemInfoStreamResponseModel'];
export type MediaItemInfoChapter = components['schemas']['MediaItemInfoChapterResponseModel'];

export function getMovie(id: number): Promise<MovieDetail> {
  return request<MovieDetail>(`/api/v1/movies/${id}`);
}

export function getShow(id: number): Promise<ShowDetail> {
  return request<ShowDetail>(`/api/v1/shows/${id}`);
}

export function getSeason(id: number): Promise<SeasonDetail> {
  return request<SeasonDetail>(`/api/v1/seasons/${id}`);
}

export function getArtist(id: number): Promise<ArtistDetail> {
  return request<ArtistDetail>(`/api/v1/artists/${id}`);
}

export function getMediaItemInfo(id: number): Promise<MediaItemInfo> {
  return request<MediaItemInfo>(`/api/v1/media-items/${id}/info`);
}

export function messageFromMediaDetailError(error: unknown, fallback = 'Unable to load media details'): string {
  if (error instanceof ApiError) {
    return error.detail ?? error.message;
  }

  if (error instanceof Error) {
    return error.message;
  }

  return fallback;
}
