<?php
// 1. Configurações
$api_key = '48c34bf50882a9a871760dbb0434a9da';
$idioma  = 'pt-BR';
$data_corte = '2025-01-01'; // Filmes de 2025 para cá

// 2. Busca os 30 filmes mais populares de 2025+
// Fazemos duas requisições (página 1 e 2) para garantir 30 filmes com qualidade
$slides = [];
$paginas_para_buscar = [1, 2];

foreach ($paginas_para_buscar as $pg) {
    $url_discover = "https://api.themoviedb.org/3/discover/movie?api_key={$api_key}&language={$idioma}&sort_by=popularity.desc&include_adult=false&primary_release_date.gte={$data_corte}&page={$pg}";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url_discover);
    $res_discover = json_decode(curl_exec($ch), true);

    if (!empty($res_discover['results'])) {
        foreach ($res_discover['results'] as $movie) {
            if (count($slides) >= 30) break; // Para quando chegar em 30

            $id = $movie['id'];
            
            // Busca Logos do filme (Prioridade PT e depois EN)
            $url_images = "https://api.themoviedb.org/3/movie/{$id}/images?api_key={$api_key}&include_image_language=pt,en,null";
            curl_setopt($ch, CURLOPT_URL, $url_images);
            $res_images = json_decode(curl_exec($ch), true);
            
            $logo_path = '';
            if (!empty($res_images['logos'])) {
                $logo_path = $res_images['logos'][0]['file_path'];
            }

            // Apenas adiciona se tiver imagem de fundo
            if (!empty($movie['backdrop_path'])) {
                $slides[] = [
                    'bg'   => "https://image.tmdb.org/t/p/original" . $movie['backdrop_path'],
                    'logo' => $logo_path ? "https://image.tmdb.org/t/p/w500" . $logo_path : ""
                ];
            }
        }
    }
    curl_close($ch);
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PowerPlay Dynamic Slider 2025</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body, html { width: 100%; height: 100%; overflow: hidden; background: #000; }

        .slider { width: 100vw; height: 100vh; position: relative; }

        .slide {
            position: absolute;
            width: 100%;
            height: 100%;
            opacity: 0;
            transition: opacity 1.2s ease-in-out;
            background-size: cover;
            background-position: center;
        }

        .slide.active { opacity: 1; }

        /* Estilo da Logo no Canto Superior Esquerdo */
        .movie-logo {
            position: absolute;
            top: 6%;
            left: 5%;
            width: 28%; 
            max-width: 450px;
            z-index: 10;
            filter: drop-shadow(0 0 15px rgba(0,0,0,0.9));
            -webkit-user-drag: none;
        }

        /* Degradê para destacar a logo */
        .overlay {
            position: absolute;
            width: 100%;
            height: 100%;
            background: linear-gradient(to bottom right, rgba(0,0,0,0.6) 0%, transparent 45%);
        }
    </style>
</head>
<body oncontextmenu="return false;">

    <div class="slider">
        <?php foreach ($slides as $index => $slide): ?>
            <div class="slide <?php echo $index === 0 ? 'active' : ''; ?>" 
                 style="background-image: url('<?php echo $slide['bg']; ?>');">
                <div class="overlay"></div>
                <?php if ($slide['logo']): ?>
                    <img src="<?php echo $slide['logo']; ?>" class="movie-logo">
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>

    <script>
        let slides = document.querySelectorAll('.slide');
        let currentSlide = 0;

        function nextSlide() {
            if(slides.length === 0) return;
            slides[currentSlide].classList.remove('active');
            currentSlide = (currentSlide + 1) % slides.length;
            slides[currentSlide].classList.add('active');
        }

        // Troca a cada 5 segundos
        setInterval(nextSlide, 5000);
    </script>

</body>
</html>