import type { MovieCatalog, ITMDBResponse } from "./interfaces";

export async function getTopMovies(): Promise<MovieCatalog[]> {
    const API_KEY = import.meta.env.TMDB_API_KEY;
    const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/w500'

    const response = await fetch(`https://api.themoviedb.org/3/discover/movie?api_key=${API_KEY}`);
    const data: ITMDBResponse = await response.json();

    return data.results.slice(0, 5).map((movie) => ({
    id: movie.id,
    title: movie.title,
    poster: `${IMAGE_BASE_URL}${movie.poster_path}`,
    rating: movie.vote_average,
    year: movie.release_date.split('-')[0], // Extracts just the year
    genre: movie.genre,
    duration: movie.duration,
    fsk: movie.age_reccomendation,
    description: movie.overview,
    backdrop: movie.backdrop_path,
  }));
}