import '../../contracts/library.dart';
import '../ports/emby_gateway.dart';


typedef SeriesResumeTarget = ({String episodeId, String? seasonId});


Future<SeriesResumeTarget?> resolveSeriesResumeTarget(
  EmbyGateway gateway,
  AuthedRequestContext ctx,
  String seriesId,
) async {
  final nextUp = await gateway.getSeriesNextUp(ctx, seriesId, limit: 1);
  if (nextUp.isNotEmpty) {
    final ep = nextUp.first;
    return (episodeId: ep.Id, seasonId: ep.SeasonId);
  }
  final lastPlayed = await gateway.getLibraryItems(
    ctx: ctx,
    parentId: seriesId,
    includeItemTypes: 'Episode',
    recursive: true,
    filters: 'IsPlayed',
    sortBy: 'DatePlayed',
    sortOrder: SortOrder.descending,
    limit: 1,
  );
  if (lastPlayed.isNotEmpty) {
    final ep = lastPlayed.first;
    return (episodeId: ep.Id, seasonId: ep.SeasonId);
  }
  return null;
}
