export type SongType = 1 | 2 | 3 | number;

export type Track = {
	/**
	 * Canonical unique identifier for playback + dedupe.
	 *
	 * Per your rules: use `annSongId` (NOT `songId`).
	 */
	id: number;

	/** Audio source URL. Typically `/cdn/${fileName}`. */
	src: string;

	/** Display metadata */
	title: string;
	artist: string;
	album: string;

	/** Optional extra context for rendering/debugging */
	animeName?: string;
	type?: SongType;
	number?: number;
	fileName?: string | null;
};

export type SongRowLike = {
	annSongId: number;
	animeName: string;
	type: SongType;
	number: number;
	songName: string;
	artistName: string | null;
	fileName?: string | null;
};

/**
 * Convert a DB/UI song row into a normalized playable Track.
 *
 * Note: returns `null` if we can't build a playable `src` (missing fileName).
 */
export function trackFromSongRow(row: SongRowLike): Track | null {
	const fileName = row.fileName ?? null;
	if (!fileName) return null;

	const artist = (row.artistName ?? "").trim() || "Unknown Artist";

	return {
		id: Number(row.annSongId),
		src: `/cdn/${fileName}`,
		title: row.songName,
		artist,
		album: row.animeName,

		animeName: row.animeName,
		type: row.type,
		number: row.number,
		fileName,
	};
}
