/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { remap as remap$ } from "../../../lib/primitives.js";
import {
  collectExtraKeys as collectExtraKeys$,
  safeParse,
} from "../../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { RFCDate } from "../../types/rfcdate.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import { Filter, Filter$inboundSchema } from "./filter.js";
import { Image, Image$inboundSchema } from "./image.js";
import { Items, Items$inboundSchema } from "./items.js";
import { Media, Media$inboundSchema } from "./media.js";
import { Sort, Sort$inboundSchema } from "./sort.js";
import { Tag, Tag$inboundSchema } from "./tag.js";

export type MediaContainerWithNestedMetadataGuid = {
  /**
   * The unique identifier for the Guid. Can be prefixed with imdb://, tmdb://, tvdb://
   *
   * @remarks
   */
  id: string;
};

/**
 * Items in a library are referred to as "metadata items." These metadata items are distinct from "media items" which represent actual instances of media that can be consumed. Consider a TV library that has a single video file in it for a particular episode of a show. The library has a single media item, but it has three metadata items: one for the show, one for the season, and one for the episode. Consider a movie library that has two video files in it: the same movie, but two different resolutions. The library has a single metadata item for the movie, but that metadata item has two media items, one for each resolution. Additionally a "media item" will have one or more "media parts" where the the parts are intended to be watched together, such as a CD1 and CD2 parts of the same movie.
 *
 * @remarks
 *
 * Note that when a metadata item has multiple media items, those media items should be isomorphic. That is, a 4K version and 1080p version of a movie are different versions of the same movie. They have the same duration, same summary, same rating, etc. and they can generally be considered interchangeable. A theatrical release vs. director's cut vs. unrated version on the other hand would be separate metadata items.
 *
 * Metadata items can often live in a hierarchy with relationships between them.  For example, the metadata item for an episodes is associated with a season metadata item which is associated with a show metadata item.  A similar hierarchy exists with track, album, and artist and photos and photo album.  The relationships may be expressed via relative terms and absolute terms.  For example, "leaves" refer to metadata items which has associated media (there is no media for a season nor show).  A show will have "children" in the form of seasons and a season will have "children" in the form of episodes and episodes have "parent" in the form of a season which has a "parent" in the form of a show.  Similarly, a show has "grandchildren" in the form of episodse and an episode has a "grandparent" in the form of a show.
 */
export type MetadataItem = {
  /**
   * The title of the item (e.g. “300” or “The Simpsons”)
   */
  title: string;
  /**
   * The type of the video item, such as `movie`, `episode`, or `clip`.
   */
  type: string;
  /**
   * When present, contains the disc number for a track on multi-disc albums.
   */
  absoluteIndex?: number | undefined;
  /**
   * In units of seconds since the epoch, returns the time at which the item was added to the library.
   */
  addedAt: number;
  /**
   * When present, the URL for the background artwork for the item.
   */
  art?: string | undefined;
  /**
   * Some rating systems separate reviewer ratings from audience ratings
   */
  audienceRating?: number | undefined;
  /**
   * A URI representing the image to be shown with the audience rating (e.g. rottentomatoes://image.rating.spilled).
   */
  audienceRatingImage?: string | undefined;
  autotag?: Array<Tag> | undefined;
  /**
   * When present, the URL for a banner graphic for the item.
   */
  banner?: string | undefined;
  /**
   * When present, indicates the source for the chapters in the media file. Can be media (the chapters were embedded in the media itself), agent (a metadata agent computed them), or mixed (a combination of the two).
   */
  chapterSource?: string | undefined;
  /**
   * The number of child items associated with this media item.
   */
  childCount?: number | undefined;
  /**
   * When present, the URL for a composite image for descendent items (e.g. photo albums or playlists).
   */
  composite?: string | undefined;
  /**
   * If known, the content rating (e.g. MPAA) for an item.
   */
  contentRating?: string | undefined;
  country?: Array<Tag> | undefined;
  director?: Array<Tag> | undefined;
  /**
   * When present, the duration for the item, in units of milliseconds.
   */
  duration?: number | undefined;
  /**
   * Typically only seen in metadata at a library's top level
   */
  filter?: Array<Filter> | undefined;
  genre?: Array<Tag> | undefined;
  /**
   * The `art` of the grandparent
   */
  grandparentArt?: string | undefined;
  /**
   * The GUID of the grandparent media item.
   */
  grandparentGuid?: string | undefined;
  /**
   * The `hero` of the grandparent
   */
  grandparentHero?: string | undefined;
  /**
   * The `key` of the grandparent
   */
  grandparentKey?: string | undefined;
  /**
   * The `ratingKey` of the grandparent
   */
  grandparentRatingKey?: string | undefined;
  /**
   * The `theme` of the grandparent
   */
  grandparentTheme?: string | undefined;
  /**
   * The `thumb` of the grandparent
   */
  grandparentThumb?: string | undefined;
  /**
   * The `title` of the grandparent
   */
  grandparentTitle?: string | undefined;
  /**
   * The globally unique identifier for the media item.
   */
  guid?: string | undefined;
  guids?: Array<MediaContainerWithNestedMetadataGuid> | undefined;
  /**
   * When present, the URL for a hero image for the item.
   */
  hero?: string | undefined;
  image?: Array<Image> | undefined;
  /**
   * When present, this represents the episode number for episodes, season number for seasons, or track number for audio tracks.
   */
  index?: number | undefined;
  /**
   * The key at which the item's details can be fetched.  In many cases a metadata item may be passed without all the details (such as in a hub) and this key corresponds to the endpoint to fetch additional details.
   */
  key: string;
  lastViewedAt?: number | undefined;
  /**
   * For shows and seasons, contains the number of total episodes.
   */
  leafCount?: number | undefined;
  media?: Array<Media> | undefined;
  /**
   * When present, in the format YYYY-MM-DD [HH:MM:SS] (the hours/minutes/seconds part is not always present). The air date, or a higher resolution release date for an item, depending on type. For example, episodes usually have air date like 1979-08-10 (we don't use epoch seconds because media existed prior to 1970). In some cases, recorded over-the-air content has higher resolution air date which includes a time component. Albums and movies may have day-resolution release dates as well.
   */
  originallyAvailableAt?: RFCDate | undefined;
  /**
   * When present, used to indicate an item's original title, e.g. a movie's foreign title.
   */
  originalTitle?: string | undefined;
  /**
   * The GUID of the parent media item.
   */
  parentGuid?: string | undefined;
  /**
   * The `hero` of the parent
   */
  parentHero?: string | undefined;
  /**
   * The `index` of the parent
   */
  parentIndex?: number | undefined;
  /**
   * The `key` of the parent
   */
  parentKey?: string | undefined;
  /**
   * The `ratingKey` of the parent
   */
  parentRatingKey?: string | undefined;
  /**
   * The `thumb` of the parent
   */
  parentThumb?: string | undefined;
  /**
   * The `title` of the parent
   */
  parentTitle?: string | undefined;
  /**
   * Indicates that the item has a primary extra; for a movie, this is a trailer, and for a music track it is a music video. The URL points to the metadata details endpoint for the item.
   */
  primaryExtraKey?: string | undefined;
  /**
   * Prompt to give the user for this directory (such as `Search Movies`)
   */
  prompt?: string | undefined;
  /**
   * When present, the rating for the item. The exact meaning and representation depends on where the rating was sourced from.
   */
  rating?: number | undefined;
  ratingArray?: Array<Tag> | undefined;
  /**
   * Number of ratings under this metadata
   */
  ratingCount?: number | undefined;
  /**
   * When present, indicates an image to be shown with the rating. This is passed back as a small set of defined URI values, e.g. rottentomatoes://image.rating.rotten.
   */
  ratingImage?: string | undefined;
  /**
   * This is the opaque string to be passed into timeline, scrobble, and rating endpoints to identify them.  While it often appears to be numeric, this is not guaranteed.
   */
  ratingKey?: string | undefined;
  role?: Array<Tag> | undefined;
  /**
   * Indicates this is a search directory
   */
  search?: boolean | undefined;
  /**
   * Used by old clients to provide nested menus allowing for primative (but structured) navigation.
   */
  secondary?: boolean | undefined;
  /**
   * When found on a show item, indicates that the children (seasons) should be skipped in favor of the grandchildren (episodes). Useful for mini-series, etc.
   */
  skipChildren?: boolean | undefined;
  /**
   * When present on an episode or track item, indicates parent should be skipped in favor of grandparent (show).
   */
  skipParent?: boolean | undefined;
  /**
   * Typically only seen in metadata at a library's top level
   */
  sort?: Array<Sort> | undefined;
  /**
   * When present, the studio or label which produced an item (e.g. movie studio for movies, record label for albums).
   */
  studio?: string | undefined;
  /**
   * The subtype of the video item, such as `photo` when the video item is in a photo library
   */
  subtype?: string | undefined;
  /**
   * When present, the extended textual information about the item (e.g. movie plot, artist biography, album review).
   */
  summary?: string | undefined;
  /**
   * When present, a pithy one-liner about the item (usually only seen for movies).
   */
  tagline?: string | undefined;
  /**
   * When present, the URL for theme music for the item (usually only for TV shows).
   */
  theme?: string | undefined;
  /**
   * When present, the URL for the poster or thumbnail for the item. When available for types like movie, it will be the poster graphic, but fall-back to the extracted media thumbnail.
   */
  thumb?: string | undefined;
  /**
   * Whene present, this is the string used for sorting the item. It's usually the title with any leading articles removed (e.g. “Simpsons”).
   */
  titleSort?: string | undefined;
  /**
   * In units of seconds since the epoch, returns the time at which the item was last changed (e.g. had its metadata updated).
   */
  updatedAt?: number | undefined;
  /**
   * When the user has rated an item, this contains the user rating
   */
  userRating?: number | undefined;
  /**
   * When a users has completed watched or listened to an item, this attribute contains the number of consumptions.
   */
  viewCount?: number | undefined;
  /**
   * For shows and seasons, contains the number of viewed episodes.
   */
  viewedLeafCount?: number | undefined;
  /**
   * When a user is in the process of viewing or listening to this item, this attribute contains the current offset, in units of milliseconds.
   */
  viewOffset?: number | undefined;
  writer?: Array<Tag> | undefined;
  /**
   * When present, the year associated with the item's release (e.g. release year for a movie).
   */
  year?: number | undefined;
  metadataItem?: Array<Items> | undefined;
  additionalProperties?: { [k: string]: any } | undefined;
};

/**
 * `MediaContainer` is the root element of most Plex API responses. It serves as a generic container for various types of content (Metadata, Hubs, Directories, etc.) and includes pagination information (offset, size, totalSize) when applicable.
 *
 * @remarks
 * Common attributes: - identifier: Unique identifier for this container - size: Number of items in this response page - totalSize: Total number of items available (for pagination) - offset: Starting index of this page (for pagination)
 * The container often "hoists" common attributes from its children. For example, if all tracks in a container share the same album title, the `parentTitle` attribute may appear on the MediaContainer rather than being repeated on each track.
 */
export type MediaContainerWithNestedMetadataMediaContainer = {
  identifier?: string | undefined;
  /**
   * The offset of where this container page starts among the total objects available. Also provided in the `X-Plex-Container-Start` header.
   *
   * @remarks
   */
  offset?: number | undefined;
  size?: number | undefined;
  /**
   * The total size of objects available. Also provided in the `X-Plex-Container-Total-Size` header.
   *
   * @remarks
   */
  totalSize?: number | undefined;
  metadataItem?: Array<MetadataItem> | undefined;
};

export type MediaContainerWithNestedMetadata = {
  mediaContainer?: MediaContainerWithNestedMetadataMediaContainer | undefined;
};

/** @internal */
export const MediaContainerWithNestedMetadataGuid$inboundSchema: z.ZodType<
  MediaContainerWithNestedMetadataGuid,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: z.string(),
});

export function mediaContainerWithNestedMetadataGuidFromJSON(
  jsonString: string,
): SafeParseResult<MediaContainerWithNestedMetadataGuid, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) =>
      MediaContainerWithNestedMetadataGuid$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'MediaContainerWithNestedMetadataGuid' from JSON`,
  );
}

/** @internal */
export const MetadataItem$inboundSchema: z.ZodType<
  MetadataItem,
  z.ZodTypeDef,
  unknown
> = collectExtraKeys$(
  z.object({
    title: z.string(),
    type: z.string(),
    absoluteIndex: z.number().int().optional(),
    addedAt: z.number().int(),
    art: z.string().optional(),
    audienceRating: z.number().optional(),
    audienceRatingImage: z.string().optional(),
    Autotag: z.array(Tag$inboundSchema).optional(),
    banner: z.string().optional(),
    chapterSource: z.string().optional(),
    childCount: z.number().int().optional(),
    composite: z.string().optional(),
    contentRating: z.string().optional(),
    Country: z.array(Tag$inboundSchema).optional(),
    Director: z.array(Tag$inboundSchema).optional(),
    duration: z.number().int().optional(),
    Filter: z.array(Filter$inboundSchema).optional(),
    Genre: z.array(Tag$inboundSchema).optional(),
    grandparentArt: z.string().optional(),
    grandparentGuid: z.string().optional(),
    grandparentHero: z.string().optional(),
    grandparentKey: z.string().optional(),
    grandparentRatingKey: z.string().optional(),
    grandparentTheme: z.string().optional(),
    grandparentThumb: z.string().optional(),
    grandparentTitle: z.string().optional(),
    guid: z.string().optional(),
    Guid: z.array(
      z.lazy(() => MediaContainerWithNestedMetadataGuid$inboundSchema),
    ).optional(),
    hero: z.string().optional(),
    Image: z.array(Image$inboundSchema).optional(),
    index: z.number().int().optional(),
    key: z.string(),
    lastViewedAt: z.number().int().optional(),
    leafCount: z.number().int().optional(),
    Media: z.array(Media$inboundSchema).optional(),
    originallyAvailableAt: z.string().transform(v => new RFCDate(v)).optional(),
    originalTitle: z.string().optional(),
    parentGuid: z.string().optional(),
    parentHero: z.string().optional(),
    parentIndex: z.number().int().optional(),
    parentKey: z.string().optional(),
    parentRatingKey: z.string().optional(),
    parentThumb: z.string().optional(),
    parentTitle: z.string().optional(),
    primaryExtraKey: z.string().optional(),
    prompt: z.string().optional(),
    rating: z.number().optional(),
    Rating: z.array(Tag$inboundSchema).optional(),
    ratingCount: z.number().int().optional(),
    ratingImage: z.string().optional(),
    ratingKey: z.string().optional(),
    Role: z.array(Tag$inboundSchema).optional(),
    search: z.boolean().optional(),
    secondary: z.boolean().optional(),
    skipChildren: z.boolean().optional(),
    skipParent: z.boolean().optional(),
    Sort: z.array(Sort$inboundSchema).optional(),
    studio: z.string().optional(),
    subtype: z.string().optional(),
    summary: z.string().optional(),
    tagline: z.string().optional(),
    theme: z.string().optional(),
    thumb: z.string().optional(),
    titleSort: z.string().optional(),
    updatedAt: z.number().int().optional(),
    userRating: z.number().optional(),
    viewCount: z.number().int().optional(),
    viewedLeafCount: z.number().int().optional(),
    viewOffset: z.number().int().optional(),
    Writer: z.array(Tag$inboundSchema).optional(),
    year: z.number().int().optional(),
    MetadataItem: z.array(Items$inboundSchema).optional(),
  }).catchall(z.any()),
  "additionalProperties",
  true,
).transform((v) => {
  return remap$(v, {
    "Autotag": "autotag",
    "Country": "country",
    "Director": "director",
    "Filter": "filter",
    "Genre": "genre",
    "Guid": "guids",
    "Image": "image",
    "Media": "media",
    "Rating": "ratingArray",
    "Role": "role",
    "Sort": "sort",
    "Writer": "writer",
    "MetadataItem": "metadataItem",
  });
});

export function metadataItemFromJSON(
  jsonString: string,
): SafeParseResult<MetadataItem, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => MetadataItem$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'MetadataItem' from JSON`,
  );
}

/** @internal */
export const MediaContainerWithNestedMetadataMediaContainer$inboundSchema:
  z.ZodType<
    MediaContainerWithNestedMetadataMediaContainer,
    z.ZodTypeDef,
    unknown
  > = z.object({
    identifier: z.string().optional(),
    offset: z.number().int().optional(),
    size: z.number().int().optional(),
    totalSize: z.number().int().optional(),
    MetadataItem: z.array(z.lazy(() => MetadataItem$inboundSchema)).optional(),
  }).transform((v) => {
    return remap$(v, {
      "MetadataItem": "metadataItem",
    });
  });

export function mediaContainerWithNestedMetadataMediaContainerFromJSON(
  jsonString: string,
): SafeParseResult<
  MediaContainerWithNestedMetadataMediaContainer,
  SDKValidationError
> {
  return safeParse(
    jsonString,
    (x) =>
      MediaContainerWithNestedMetadataMediaContainer$inboundSchema.parse(
        JSON.parse(x),
      ),
    `Failed to parse 'MediaContainerWithNestedMetadataMediaContainer' from JSON`,
  );
}

/** @internal */
export const MediaContainerWithNestedMetadata$inboundSchema: z.ZodType<
  MediaContainerWithNestedMetadata,
  z.ZodTypeDef,
  unknown
> = z.object({
  MediaContainer: z.lazy(() =>
    MediaContainerWithNestedMetadataMediaContainer$inboundSchema
  ).optional(),
}).transform((v) => {
  return remap$(v, {
    "MediaContainer": "mediaContainer",
  });
});

export function mediaContainerWithNestedMetadataFromJSON(
  jsonString: string,
): SafeParseResult<MediaContainerWithNestedMetadata, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => MediaContainerWithNestedMetadata$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'MediaContainerWithNestedMetadata' from JSON`,
  );
}
