addToWatch method

Future<void> addToWatch(
  1. Movie movie
)

Adds a movie to the to-watch list and saves to POD.

Implementation

Future<void> addToWatch(Movie movie) async {
  // If toWatchListId is null, try to initialise it first.

  if (_toWatchListId == null) {
    await _retryInitializeMovieListIds();
  }

  // Only use MovieList - remove old TTL operations.
  if (_toWatchListId != null) {
    final success =
        await _movieListService.addMovieToList(_toWatchListId!, movie);
    if (success) {
      // Update stream with fresh data from MovieList.
      final movies = await getToWatch(forceRefresh: true);
      _toWatchController.add(movies);

      // DO NOT call _createOrUpdateMovieFile() here to avoid race condition.
      // The movie file should already be created/updated by the caller.
      return;
    } else {
      debugPrint('❌ Failed to add ${movie.title} to To Watch MovieList');
    }
  } else {
    debugPrint('❌ To Watch list ID is null, cannot add movie to MovieList');
  }

  // Fallback to old system if MovieList fails.
  final toWatch = await getToWatch();
  if (!toWatch.any((m) => m.id == movie.id)) {
    toWatch.add(movie);
    await _saveToWatchToPod(toWatch);
    _toWatchController.add(toWatch);
    // DO NOT call _createOrUpdateMovieFile() here to avoid race condition.
  }
}