import { z } from "zod";

// Supported codec enums
export const VIDEO_CODECS = ["H264", "H265", "VP9", "AV1"] as const;
export const AUDIO_CODECS = ["AAC", "MP3", "OPUS", "VORBIS"] as const;
export const RESOLUTIONS = ["480p", "720p", "1080p", "2160p"] as const;
export const PLAYBACK_STATUS = [
	"PREPARING",
	"READY",
	"STREAMING",
	"STOPPED",
	"ERROR",
] as const;

export type VideoCodec = (typeof VIDEO_CODECS)[number];
export type AudioCodec = (typeof AUDIO_CODECS)[number];
export type Resolution = (typeof RESOLUTIONS)[number];
export type PlaybackStatus = (typeof PLAYBACK_STATUS)[number];

// Input Schemas
export const CreatePlaybackSessionInput = z
	.object({
		titleId: z.string().optional(),
		episodeId: z.string().optional(),
		supportedVideoCodecs: z.array(z.enum(VIDEO_CODECS)),
		supportedAudioCodecs: z.array(z.enum(AUDIO_CODECS)),
		audioStreamIndex: z.number().optional(),
		subtitleStreamIndex: z.number().optional(),
		burnInSubtitle: z.boolean().optional().default(false),
		maxResolution: z.enum(RESOLUTIONS).default("1080p"),
		startOffset: z.number().min(0).optional(), // Start playback from timestamp (seconds)
	})
	.refine((data) => data.titleId || data.episodeId, {
		message: "Either titleId or episodeId must be provided",
	});

export const GetPlaybackSessionInput = z.object({
	sessionId: z.string(),
});

export const StopPlaybackSessionInput = z.object({
	sessionId: z.string(),
});

export const SeekPlaybackSessionInput = z.object({
	sessionId: z.string(),
	seekTime: z.number().min(0), // Seek to timestamp (seconds)
});

// Output Schemas
export const CreatePlaybackSessionOutput = z.object({
	sessionId: z.string(),
	playlistUrl: z.string(),
	status: z.enum(PLAYBACK_STATUS),
	message: z.string().optional(),
});

export const PlaybackSessionStatusOutput = z.object({
	sessionId: z.string(),
	status: z.enum(PLAYBACK_STATUS),
	startedAt: z.date(),
	lastAccessedAt: z.date(),
	isTranscoding: z.boolean(),
	currentOffset: z.number(),
	isRestarting: z.boolean(),
});

export const StopPlaybackSessionOutput = z.object({
	success: z.boolean(),
	message: z.string().optional(),
});

export const SeekPlaybackSessionOutput = z.object({
	success: z.boolean(),
	newOffset: z.number(),
	message: z.string().optional(),
});

export const ListPlaybackSessionsOutput = z.array(PlaybackSessionStatusOutput);

// Type exports
export type CreatePlaybackSessionInput = z.infer<
	typeof CreatePlaybackSessionInput
>;
export type CreatePlaybackSessionOutput = z.infer<
	typeof CreatePlaybackSessionOutput
>;
export type GetPlaybackSessionInput = z.infer<typeof GetPlaybackSessionInput>;
export type PlaybackSessionStatusOutput = z.infer<
	typeof PlaybackSessionStatusOutput
>;
export type StopPlaybackSessionInput = z.infer<typeof StopPlaybackSessionInput>;
export type StopPlaybackSessionOutput = z.infer<
	typeof StopPlaybackSessionOutput
>;
export type SeekPlaybackSessionInput = z.infer<typeof SeekPlaybackSessionInput>;
export type SeekPlaybackSessionOutput = z.infer<
	typeof SeekPlaybackSessionOutput
>;
export type ListPlaybackSessionsOutput = z.infer<
	typeof ListPlaybackSessionsOutput
>;
