<?php
// ==========================================
// 1. TELEGRAM AUTOMATION TRIGGER (TOP PRIORITY)
// ==========================================
if(file_exists('config.php')) {
    require_once 'config.php';
} elseif(file_exists('../config.php')) {
    require_once '../config.php';
}

// লজিক ফাইল লোড করা
if(file_exists('admin/tg_logic_core.php')) {
    require_once 'admin/tg_logic_core.php';
} elseif(file_exists('../admin/tg_logic_core.php')) {
    require_once '../admin/tg_logic_core.php';
}

// অটোমেশন রান করা
if(isset($conn) && function_exists('runNameChangeCheck')) {
    runNameChangeCheck($conn);
    if(function_exists('runWeeklyReminder')) runWeeklyReminder($conn);
}

// ==========================================
// 2. YOUTUBE VIDEO LOGIC (DB FETCH)
// ==========================================
// ডাটাবেস থেকে লিংক আনা
$bgm_query = $conn->query("SELECT setting_value FROM settings WHERE setting_key='youtube_bgm_link'");
$bgm_row = $bgm_query->fetch_assoc();
$youtube_link = !empty($bgm_row['setting_value']) ? $bgm_row['setting_value'] : "https://youtu.be/WB4d7hOauww"; 

// লিংক থেকে ভিডিও আইডি বের করা
$video_id = "";
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $youtube_link, $match)) {
    $video_id = $match[1];
}
?>

<!-- HTML & CSS START -->
<style>
    /* === HEADER BASIC === */
    header {
        position: sticky; top: 0; z-index: 1000;
        background: rgba(5, 5, 5, 0.95);
        backdrop-filter: blur(10px);
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: 0 4px 20px rgba(0,0,0,0.8);
    }
    .nav-container {
        max-width: 1200px; margin: 0 auto; padding: 0 20px;
        display: flex; justify-content: space-between; align-items: center;
        height: 65px; position: relative; z-index: 1001;
    }
    .logo { text-decoration: none; font-family: sans-serif; font-weight: 900; font-size: 24px; color: #fff; letter-spacing: 1px; }
    .logo span { color: #00f2ff; text-shadow: 0 0 10px #00f2ff; }
    .right-wrapper { display: flex; align-items: center; gap: 15px; }

    /* === MUSIC VISUALIZER === */
    .music-bar { display: none; gap: 3px; align-items: flex-end; height: 20px; width: 25px; }
    .bar { width: 3px; background: #00ff9d; animation: equalizer 1s infinite ease-in-out; }
    .bar:nth-child(1) { height: 10px; animation-duration: 0.8s; }
    .bar:nth-child(2) { height: 20px; animation-duration: 1.1s; }
    .bar:nth-child(3) { height: 15px; animation-duration: 0.9s; }
    @keyframes equalizer { 0%, 100% { transform: scaleY(1); } 50% { transform: scaleY(0.5); } }

    /* === ICONS === */
    .nav-menu { display: flex; gap: 12px; align-items: center; }
    .nav-menu a {
        width: 35px; height: 35px; display: flex; align-items: center; justify-content: center;
        border-radius: 50%; text-decoration: none; font-size: 18px; font-weight: bold;
        transition: all 0.3s ease; border: 1px solid rgba(255,255,255,0.1);
    }
    .nav-menu a:nth-child(1) { background: #5865F2; color: #fff; box-shadow: 0 0 10px rgba(88, 101, 242, 0.4); } 
    .nav-menu a:nth-child(2) { background: #FF0000; color: #fff; box-shadow: 0 0 10px rgba(255, 0, 0, 0.4); } 
    .nav-menu a:nth-child(3) { background: #1877F2; color: #fff; box-shadow: 0 0 10px rgba(24, 119, 242, 0.4); } 
    .nav-menu a:nth-child(4) { background: linear-gradient(45deg, #00f2ea, #ff0050); color: #fff; box-shadow: 0 0 10px rgba(255, 0, 80, 0.4); } 
    
    .nav-menu a:hover {
        background: #ffffff !important; transform: translateY(-3px) scale(1.1);
        box-shadow: 0 0 20px #ffffff, 0 0 40px rgba(255,255,255,0.5) !important;
    }
    .nav-menu a:nth-child(1):hover { color: #5865F2; } 
    .nav-menu a:nth-child(2):hover { color: #FF0000; }
    .nav-menu a:nth-child(3):hover { color: #1877F2; } 
    .nav-menu a:nth-child(4):hover { color: #000000; }

    @media (max-width: 768px) {
        .nav-container { padding: 0 15px; height: 60px; }
        .logo { font-size: 20px; }
        .nav-menu a { width: 32px; height: 32px; font-size: 16px; }
    }

    #snowCanvas { position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 99999; }
    #player { position: absolute; top: -9999px; visibility: hidden; } 
</style>

<!-- Snow & Player Elements -->
<canvas id="snowCanvas"></canvas>
<div id="player"></div>

<!-- Navigation Header -->
<header>
    <div class="nav-container">
        <a href="index.php" class="logo">TAMIM<span>1280</span></a>
        <div class="right-wrapper">
            <!-- Music Animation -->
            <div id="musicIndicator" class="music-bar">
                <div class="bar"></div><div class="bar"></div><div class="bar"></div>
            </div>
            <!-- Social Menu -->
            <nav class="nav-menu">
                <a href="https://discord.com/invite/C4U7uB6Gvs" target="_blank"><i class="fa-brands fa-discord"></i></a>
                <a href="https://www.youtube.com/@tamim_1280" target="_blank"><i class="fa-brands fa-youtube"></i></a>
                <a href="#" target="_blank"><i class="fa-brands fa-facebook-f"></i></a>
                <a href="https://www.tiktok.com/@tamim1280_official" target="_blank"><i class="fa-brands fa-tiktok"></i></a>
            </nav>
        </div>
    </div>
</header>

<!-- Scripts -->
<script src="https://www.youtube.com/iframe_api"></script>
<script>
    // --- SMART MUSIC MEMORY LOGIC ---
    var player;
    var savedTime = localStorage.getItem('guild_bgm_time') || 0;
    
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            height: '10', width: '10',
            videoId: '<?php echo $video_id; ?>',
            playerVars: { 
                'autoplay': 1, 'controls': 0, 'loop': 1, 'playlist': '<?php echo $video_id; ?>',
                'start': parseInt(savedTime), 'playsinline': 1 
            },
            events: { 'onReady': onPlayerReady, 'onStateChange': onStateChange }
        });
    }

    function onPlayerReady(event) {
        event.target.playVideo(); 
        var savedVol = localStorage.getItem('guild_bgm_vol');
        if(savedVol) event.target.setVolume(savedVol); else event.target.setVolume(100);
        setInterval(saveMusicState, 1000);
    }

    function saveMusicState() {
        if(player && player.getCurrentTime) {
            localStorage.setItem('guild_bgm_time', player.getCurrentTime());
            localStorage.setItem('guild_bgm_vol', player.getVolume());
        }
    }

    function forcePlay() { if(player && player.getPlayerState() !== 1) { player.playVideo(); } }
    document.addEventListener('click', forcePlay);
    document.addEventListener('scroll', forcePlay);
    document.addEventListener('touchstart', forcePlay);

    function onStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING) {
            document.getElementById("musicIndicator").style.display = "flex";
        }
    }

    // --- SNOWFALL EFFECT ---
    (function() {
        var canvas = document.getElementById('snowCanvas'), ctx = canvas.getContext('2d');
        var width = canvas.width = window.innerWidth, height = canvas.height = window.innerHeight;
        var particles = [], maxP = window.innerWidth < 768 ? 40 : 80;
        window.addEventListener('resize', function() { width = canvas.width = window.innerWidth; height = canvas.height = window.innerHeight; });
        function Particle() { this.x = Math.random() * width; this.y = Math.random() * height; this.vx = Math.random() - 0.5; this.vy = Math.random() * 2 + 1; this.size = Math.random() * 2 + 1; this.op = Math.random() * 0.5 + 0.3; }
        Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; if (this.y > height) { this.y = -10; this.x = Math.random() * width; } if (this.x > width) this.x=0; if(this.x<0) this.x=width; };
        Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fillStyle = "rgba(255,255,255," + this.op + ")"; ctx.fill(); };
        function init() { for(var i=0; i<maxP; i++) particles.push(new Particle()); animate(); }
        function animate() { ctx.clearRect(0, 0, width, height); for(var i=0; i<particles.length; i++) { particles[i].update(); particles[i].draw(); } requestAnimationFrame(animate); }
        init();
    })();
</script>

<?php 
if(file_exists('security.php')) {
    require_once 'security.php'; 
    if(function_exists('get_complete_security')) echo get_complete_security(); 
}
?>