import { z } from "zod";

// Input Schemas
export const GetMediaTracksInput = z
	.object({
		titleId: z.string().optional(),
		episodeId: z.string().optional(),
	})
	.refine((data) => data.titleId || data.episodeId, {
		message: "Either titleId or episodeId must be provided",
	});

// Output Schemas
const MediaItemSchema = z.object({
	id: z.number(),
	path: z.string(),
	container: z.string().nullable(),
	sizeBytes: z.number().nullable(),
});

const VideoTrackSchema = z.object({
	streamIndex: z.number(),
	codecName: z.string().nullable(),
	codecLongName: z.string().nullable(),
	profile: z.string().nullable(),
	width: z.number().nullable(),
	height: z.number().nullable(),
	frameRate: z.number().nullable(),
	bitRate: z.number().nullable(),
});

const AudioTrackSchema = z.object({
	streamIndex: z.number(),
	codecName: z.string().nullable(),
	codecLongName: z.string().nullable(),
	language: z.string().nullable(),
	title: z.string().nullable(),
	channels: z.number().nullable(),
	channelLayout: z.string().nullable(),
	sampleRate: z.number().nullable(),
	bitRate: z.number().nullable(),
	isDefault: z.boolean(),
});

const SubtitleTrackSchema = z.object({
	streamIndex: z.number(),
	codecName: z.string().nullable(),
	codecLongName: z.string().nullable(),
	language: z.string().nullable(),
	title: z.string().nullable(),
	isDefault: z.boolean(),
	isForced: z.boolean(),
});

export const GetMediaTracksOutput = z.array(
	z.object({
		mediaItem: MediaItemSchema,
		tracks: z.object({
			video: z.array(VideoTrackSchema),
			audio: z.array(AudioTrackSchema),
			subtitle: z.array(SubtitleTrackSchema),
		}),
	}),
);

// Type exports
export type GetMediaTracksInput = z.infer<typeof GetMediaTracksInput>;
export type GetMediaTracksOutput = z.infer<typeof GetMediaTracksOutput>;
export type VideoTrack = z.infer<typeof VideoTrackSchema>;
export type AudioTrack = z.infer<typeof AudioTrackSchema>;
export type SubtitleTrack = z.infer<typeof SubtitleTrackSchema>;
