import '../../core/contracts/library.dart';
import '../../providers/di_providers.dart';

String tmdbIdFromItem(EmbyRawItem item) {
  final rawProviderIds = item.extra['ProviderIds'];
  if (rawProviderIds is! Map) return '';

  for (final entry in rawProviderIds.entries) {
    final key = entry.key?.toString().trim().toLowerCase() ?? '';
    final value = entry.value?.toString().trim() ?? '';
    if (key == 'tmdb' && value.isNotEmpty) return value;
  }
  return '';
}

TmdbMediaKey? tmdbMediaKeyFrom(EmbyRawItem item) {
  final mediaType = switch (item.Type) {
    'Movie' => 'movie',
    'Series' || 'Episode' || 'Season' => 'tv',
    _ => null,
  };
  if (mediaType == null) return null;

  final tmdbId = tmdbIdFromItem(item);
  if (tmdbId.isEmpty) return null;

  return (tmdbId: tmdbId, mediaType: mediaType);
}
