import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/movie.dart';

class ApiService {
  static const String apiKey = '70101da29bbd7fafb66f0ed06dd88794';
  static const String accessToken = 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI3MDEwMWRhMjliYmQ3ZmFmYjY2ZjBlZDA2ZGQ4ODc5NCIsIm5iZiI6MTc0NzgxNDY5MS44NDA5OTk4LCJzdWIiOiI2ODJkODkyMzYyOTliNjFlNzM2NDk2ODkiLCJzY29wZXMiOlsiYXBpX3JlYWQiXSwidmVyc2lvbiI6MX0.nGl7SvvrI6FlcQ7uOGJzrejN-_s7G0kdvnVCzSGGwm8';
  static const String baseUrl = 'https://api.themoviedb.org/3';
  static const String imageBaseUrl = 'https://image.tmdb.org/t/p/w500';

  static Future<List<Movie>> getPopularMovies() async {
    try {
      final response = await http.get(
        Uri.parse('$baseUrl/movie/popular?api_key=$apiKey'),
        headers: {
          'Authorization': 'Bearer $accessToken',
          'Content-Type': 'application/json',
        },
      );

      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        final List<dynamic> movies = data['results'];
        
        return movies.map((movie) => Movie.fromJson(movie)).toList();
      } else {
        throw Exception('Échec du chargement des films: ${response.statusCode}');
      }
    } catch (e) {
      throw Exception('Erreur lors de la récupération des films: $e');
    }
  }

  static Future<List<Movie>> getTopRatedMovies() async {
    try {
      final response = await http.get(
        Uri.parse('$baseUrl/movie/top_rated?api_key=$apiKey'),
        headers: {
          'Authorization': 'Bearer $accessToken',
          'Content-Type': 'application/json',
        },
      );

      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        final List<dynamic> movies = data['results'];
        
        return movies.map((movie) => Movie.fromJson(movie)).toList();
      } else {
        throw Exception('Échec du chargement des films: ${response.statusCode}');
      }
    } catch (e) {
      throw Exception('Erreur lors de la récupération des films: $e');
    }
  }

  static Future<List<Movie>> getNowPlayingMovies() async {
    try {
      final response = await http.get(
        Uri.parse('$baseUrl/movie/now_playing?api_key=$apiKey'),
        headers: {
          'Authorization': 'Bearer $accessToken',
          'Content-Type': 'application/json',
        },
      );

      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        final List<dynamic> movies = data['results'];
        
        return movies.map((movie) => Movie.fromJson(movie)).toList();
      } else {
        throw Exception('Échec du chargement des films: ${response.statusCode}');
      }
    } catch (e) {
      throw Exception('Erreur lors de la récupération des films: $e');
    }
  }

  static Future<List<Movie>> getUpcomingMovies() async {
    try {
      final response = await http.get(
        Uri.parse('$baseUrl/movie/upcoming?api_key=$apiKey'),
        headers: {
          'Authorization': 'Bearer $accessToken',
          'Content-Type': 'application/json',
        },
      );

      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        final List<dynamic> movies = data['results'];
        
        return movies.map((movie) => Movie.fromJson(movie)).toList();
      } else {
        throw Exception('Échec du chargement des films: ${response.statusCode}');
      }
    } catch (e) {
      throw Exception('Erreur lors de la récupération des films: $e');
    }
  }
}
