"""Search movie use case.""" import logging from infrastructure.api.tmdb import ( TMDBAPIError, TMDBClient, TMDBConfigurationError, TMDBNotFoundError, ) from .dto import SearchMovieResponse logger = logging.getLogger(__name__) class SearchMovieUseCase: """ Use case for searching a movie and retrieving its IMDb ID. This orchestrates the TMDB API client to find movie information. """ def __init__(self, tmdb_client: TMDBClient): """ Initialize use case. Args: tmdb_client: TMDB API client """ self.tmdb_client = tmdb_client def execute(self, media_title: str) -> SearchMovieResponse: """ Search for a movie by title. Args: media_title: Title of the movie to search for Returns: SearchMovieResponse with movie information or error """ try: # Use the TMDB client to search for media result = self.tmdb_client.search_media(media_title) # Check if IMDb ID was found if result.imdb_id: logger.info(f"IMDb ID found for '{media_title}': {result.imdb_id}") return SearchMovieResponse( status="ok", imdb_id=result.imdb_id, title=result.title, media_type=result.media_type, tmdb_id=result.tmdb_id, overview=result.overview, release_date=result.release_date, vote_average=result.vote_average, ) else: logger.warning(f"No IMDb ID available for '{media_title}'") return SearchMovieResponse( status="ok", title=result.title, media_type=result.media_type, tmdb_id=result.tmdb_id, error="no_imdb_id", message=f"No IMDb ID available for '{result.title}'", ) except TMDBNotFoundError as e: logger.info(f"Media not found: {e}") return SearchMovieResponse( status="error", error="not_found", message=str(e) ) except TMDBConfigurationError as e: logger.error(f"TMDB configuration error: {e}") return SearchMovieResponse( status="error", error="configuration_error", message=str(e) ) except TMDBAPIError as e: logger.error(f"TMDB API error: {e}") return SearchMovieResponse( status="error", error="api_error", message=str(e) ) except ValueError as e: logger.error(f"Validation error: {e}") return SearchMovieResponse( status="error", error="validation_failed", message=str(e) )