import type {
	AudioCodec,
	Resolution,
	VideoCodec,
} from "@/models/trpc/playback";

// Codec name mapping from ffprobe to our standard names
const VIDEO_CODEC_MAP: Record<string, VideoCodec> = {
	h264: "H264",
	avc: "H264",
	x264: "H264",
	h265: "H265",
	hevc: "H265",
	x265: "H265",
	vp9: "VP9",
	av1: "AV1",
};

const AUDIO_CODEC_MAP: Record<string, AudioCodec> = {
	aac: "AAC",
	mp3: "MP3",
	opus: "OPUS",
	vorbis: "VORBIS",
	libopus: "OPUS",
	libvorbis: "VORBIS",
};

export function normalizeVideoCodec(
	codecName: string | null,
): VideoCodec | null {
	if (!codecName) return null;
	const normalized = codecName.toLowerCase();
	return VIDEO_CODEC_MAP[normalized] || null;
}

export function normalizeAudioCodec(
	codecName: string | null,
): AudioCodec | null {
	if (!codecName) return null;
	const normalized = codecName.toLowerCase();
	return AUDIO_CODEC_MAP[normalized] || null;
}

export function isVideoCodecSupported(
	codecName: string | null,
	supportedCodecs: VideoCodec[],
): boolean {
	const normalized = normalizeVideoCodec(codecName);
	return normalized ? supportedCodecs.includes(normalized) : false;
}

export function isAudioCodecSupported(
	codecName: string | null,
	supportedCodecs: AudioCodec[],
): boolean {
	const normalized = normalizeAudioCodec(codecName);
	return normalized ? supportedCodecs.includes(normalized) : false;
}

export function selectBestVideoCodec(
	supportedCodecs: VideoCodec[],
): VideoCodec {
	// Priority: H264 (most compatible) > H265 > VP9 > AV1
	const priority: VideoCodec[] = ["H264", "H265", "VP9", "AV1"];
	for (const codec of priority) {
		if (supportedCodecs.includes(codec)) {
			return codec;
		}
	}
	return "H264"; // fallback to most compatible
}

export function selectBestAudioCodec(
	supportedCodecs: AudioCodec[],
): AudioCodec {
	// Priority: AAC (most compatible) > OPUS > MP3 > VORBIS
	const priority: AudioCodec[] = ["AAC", "OPUS", "MP3", "VORBIS"];
	for (const codec of priority) {
		if (supportedCodecs.includes(codec)) {
			return codec;
		}
	}
	return "AAC"; // fallback to most compatible
}

export function getFFmpegVideoCodec(codec: VideoCodec): string {
	const codecMap: Record<VideoCodec, string> = {
		H264: "libx264",
		H265: "libx265",
		VP9: "libvpx-vp9",
		AV1: "libaom-av1",
	};
	return codecMap[codec];
}

export function getFFmpegAudioCodec(codec: AudioCodec): string {
	const codecMap: Record<AudioCodec, string> = {
		AAC: "aac",
		MP3: "libmp3lame",
		OPUS: "libopus",
		VORBIS: "libvorbis",
	};
	return codecMap[codec];
}

export function getResolutionHeight(resolution: Resolution): number {
	const resolutionMap: Record<Resolution, number> = {
		"480p": 480,
		"720p": 720,
		"1080p": 1080,
		"2160p": 2160,
	};
	return resolutionMap[resolution];
}

export function shouldScaleVideo(
	sourceHeight: number | null,
	maxResolution: Resolution,
): boolean {
	if (!sourceHeight) return false;
	const maxHeight = getResolutionHeight(maxResolution);
	return sourceHeight > maxHeight;
}

export function getScaleFilter(maxResolution: Resolution): string {
	const height = getResolutionHeight(maxResolution);
	return `scale=-2:${height}`;
}
