/*
 * Kyoo - A portable and vast media library solution.
 * Copyright (c) Kyoo.
 *
 * See AUTHORS.md and LICENSE file in the project root for full license information.
 *
 * Kyoo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Kyoo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
 */

import { z } from "zod";
import { imageFn } from "../traits";

/**
 * A audio or subtitle track.
 */
export const TrackP = z.object({
	/**
	 * The index of this track on the episode.
	 */
	index: z.number(),
	/**
	 * The title of the stream.
	 */
	title: z.string().nullable(),
	/**
	 * The language of this stream (as a ISO-639-2 language code)
	 */
	language: z.string().nullable(),
	/**
	 * The codec of this stream.
	 */
	codec: z.string(),
	/**
	 * Is this stream the default one of it's type?
	 */
	isDefault: z.boolean(),
	/**
	 * Is this stream tagged as forced?
	 */
	isForced: z.boolean(),
});
export type Audio = z.infer<typeof TrackP>;

export const SubtitleP = TrackP.extend({
	/*
	 * The url of this track (only if this is a subtitle)..
	 */
	link: z.string().transform(imageFn).nullable(),
});
export type Subtitle = z.infer<typeof SubtitleP>;

export const ChapterP = z.object({
	/**
	 * The start time of the chapter (in second from the start of the episode).
	 */
	startTime: z.number(),
	/**
	 * The end time of the chapter (in second from the start of the episode).
	 */
	endTime: z.number(),
	/**
	 * The name of this chapter. This should be a human-readable name that could be presented to the
	 * user. There should be well-known chapters name for commonly used chapters. For example, use
	 * "Opening" for the introduction-song and "Credits" for the end chapter with credits.
	 */
	name: z.string(),
});
export type Chapter = z.infer<typeof ChapterP>;

/**
 * The transcoder's info for this item. This include subtitles, fonts, chapters...
 */
export const WatchInfoP = z.object({
	/**
	 * The sha1 of the video file.
	 */
	sha: z.string(),
	/**
	 * The internal path of the video file.
	 */
	path: z.string(),
	/**
	 * The container of the video file of this episode. Common containers are mp4, mkv, avi and so on.
	 */
	container: z.string(),
	/**
	 * The list of audio tracks.
	 */
	audios: z.array(TrackP),
	/**
	 * The list of subtitles tracks.
	 */
	subtitles: z.array(SubtitleP),
	/**
	 * The list of fonts that can be used to display subtitles.
	 */
	fonts: z.array(z.string().transform(imageFn)),
	/**
	 * The list of chapters. See Chapter for more information.
	 */
	chapters: z.array(ChapterP),
});

/**
 * A watch info for a video
 */
export type WatchInfo = z.infer<typeof WatchInfoP>;
