<?php
// ===== CONFIGURAÇÃO DO CAMINHO DO BANCO =====
$db_path = __DIR__ . '/db/db_ads3.db';

if (!file_exists(__DIR__ . '/db')) {
    mkdir(__DIR__ . '/db', 0755, true);
}

try {
    $pdo = new PDO("sqlite:" . $db_path);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Erro ao conectar ao banco.");
}

$stmt = $pdo->query("SELECT * FROM ads ORDER BY RANDOM()");
$ads = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (empty($ads)) {
    exit('<body style="background:#000;color:#fff;display:flex;justify-content:center;align-items:center;height:100vh;font-family:sans-serif;"><h1>Aguardando Banners...</h1></body>');
}

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Display Banners 2026</title>
    <style>
        /* Reset total para modo tela cheia */
        * { margin: 0; padding: 0; box-sizing: border-box; -webkit-touch-callout: none; user-select: none; }
        html, body { background-color: #000; overflow: hidden; width: 100%; height: 100%; position: fixed; }
        
        .slideshow-container { position: relative; width: 100vw; height: 100vh; }
        
        .slide {
            position: absolute; top: 0; left: 0; width: 100%; height: 100%;
            opacity: 0; transition: opacity 0.8s ease-in-out;
            display: flex; justify-content: center; align-items: center;
        }
        
        .slide.active { opacity: 1; z-index: 10; }
        
        .media-container { width: 100%; height: 100%; }
        .media-container img, .media-container video {
            width: 100%; height: 100%; object-fit: cover; pointer-events: none;
        }
        
        /* CAMADA PRETA - COMPRIMENTO REDUZIDO */
        .text-container {
            position: absolute; 
            bottom: 0; 
            left: 0; 
            width: 100%; 
            z-index: 20;
            padding: 8px 10px; /* Padding reduzido para diminuir o comprimento vertical */
            pointer-events: none;
            background-color: rgba(0, 0, 0, 0.6); 
            backdrop-filter: blur(4px);
            -webkit-backdrop-filter: blur(4px);
        }
        
        /* TEXTO DO TITULO - TAMANHO REDUZIDO */
        .title-text {
            font-size: 0.95rem; /* Diminuído para manter ajuste perfeito no fundo menor */
            font-weight: bold; 
            text-align: center;
            text-transform: uppercase; 
            letter-spacing: 0.8px;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
            line-height: 1.2;
        }

        .loading { position: fixed; inset: 0; background: #000; display: flex; justify-content: center; align-items: center; z-index: 1000; transition: 0.5s; }
        .spinner { width: 40px; height: 40px; border: 4px solid #333; border-top-color: #d946ef; border-radius: 50%; animation: spin 1s linear infinite; }
        @keyframes spin { to { transform: rotate(360deg); } }
    </style>
</head>
<body oncontextmenu="return false;">

    <div class="loading" id="loading"><div class="spinner"></div></div>
    
    <div class="slideshow-container" id="slideshow">
        <?php foreach ($ads as $index => $ad): ?>
        <div class="slide <?= $index === 0 ? 'active' : ''; ?>">
            <div class="media-container">
                <?php
                $url = $ad['url'];
                $ext = strtolower(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION));
                $is_video = in_array($ext, ['mp4', 'webm', 'ogg']);
                
                if ($is_video): ?>
                    <video autoplay muted loop playsinline disablePictureInPicture>
                        <source src="<?= htmlspecialchars($url) ?>" type="video/<?= $ext ?>">
                    </video>
                <?php else: ?>
                    <img src="<?= htmlspecialchars($url) ?>" alt="Banner">
                <?php endif; ?>
            </div>
            
            <?php if (!empty($ad['display_title'])): ?>
            <div class="text-container">
                <div class="title-text" style="color: <?= htmlspecialchars($ad['title_color'] ?: '#ffffff'); ?>;">
                    <?= htmlspecialchars($ad['display_title']); ?>
                </div>
            </div>
            <?php endif; ?>
        </div>
        <?php endforeach; ?>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const slides = document.querySelectorAll('.slide');
            const loading = document.getElementById('loading');
            let current = 0;
            const delay = 5000; 

            window.onload = () => { 
                loading.style.opacity = '0';
                setTimeout(() => loading.style.display = 'none', 500);
            };

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

                const video = slides[current].querySelector('video');
                if (video) { 
                    video.currentTime = 0; 
                    video.play().catch(e => console.log("Autoplay bloqueado")); 
                }
            }

            if (slides.length > 1) { 
                setInterval(nextSlide, delay); 
            }

            const block = (e) => { e.preventDefault(); return false; };
            ['touchstart', 'touchmove', 'click', 'contextmenu'].forEach(ev => {
                document.addEventListener(ev, block, { passive: false });
            });
        });
    </script>
</body>
</html>