<?php
$conn = new mysqli('localhost', 'tamimsho_download', 'tamimsho_download', 'tamimsho_download');
$conn->set_charset("utf8mb4");

$video_url = isset($_POST['video_url']) ? $_POST['video_url'] : '';
if(empty($video_url)){ header("Location: index.php"); exit(); }

// ১. API Call
$options = [
    'http' => [
        'method' => 'POST',
        'header' => [
            "Content-Type: application/json",
            "x-rapidapi-host: social-download-all-in-one.p.rapidapi.com",
            "x-rapidapi-key: 29f8c3b79amshc7b9755d426320dp1b94fajsn62b1821d47db"
        ],
        'content' => json_encode(['url' => $video_url])
    ]
];
$context = stream_context_create($options);
$response = @file_get_contents('https://social-download-all-in-one.p.rapidapi.com/v1/social/autolink', false, $context);
$data = json_decode($response, true);

// ২. অটোমেটিক কোয়ালিটি নেম ডাটাবেসে সেভ করা
if($data && isset($data['medias'])){
    foreach($data['medias'] as $media){
        $q_name = mysqli_real_escape_string($conn, $media['quality']);
        $m_type = mysqli_real_escape_string($conn, $media['type']); 
        $conn->query("INSERT IGNORE INTO quality_settings (quality_name, type) VALUES ('$q_name', '$m_type')");
    }
}

// ৩. অ্যাডমিন সেটিংস লোড করা
$settings_res = $conn->query("SELECT * FROM quality_settings");
$q_settings = [];
while($r = $settings_res->fetch_assoc()){
    // কী-কে ছোট হাতের অক্ষরে রাখছি যাতে ম্যাচিং সহজ হয়
    $q_settings[strtolower($r['quality_name'])] = $r;
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Download Results - DOWNLOAD 1280</title>
    <style>
        body { background: #000; color: #fff; font-family: 'Segoe UI', Tahoma, sans-serif; text-align: center; padding: 10px; margin: 0; }
        .wrapper { max-width: 1100px; margin: 20px auto; padding: 25px; background: #050505; border-radius: 20px; border-top: 6px solid #00FFFF; border-right: 6px solid #00FFFF; border-bottom: 6px solid #FF007F; border-left: 6px solid #FF007F; }
        .title { color: #00FFFF; font-size: 20px; margin-bottom: 30px; font-weight: bold; }
        
        .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 20px; }

        /* কার্ড ডিজাইন */
        .card { background: #0f0f0f; padding: 15px; border-radius: 12px; text-decoration: none; display: flex; align-items: center; position: relative; overflow: hidden; border: 2px solid #222; transition: 0.3s; }
        .video-card { border-color: #00FFFF; }
        .audio-card { border-color: #FF007F; }
        .card:hover { transform: translateY(-5px); background: #111; }

        .icon { font-size: 30px; margin-right: 15px; }
        .info { text-align: left; flex-grow: 1; }
        .info span { display: block; font-size: 10px; color: #888; text-transform: uppercase; }
        .info b { font-size: 14px; color: #fff; text-transform: uppercase; }

        /* প্রিমিয়াম ওভারলে */
        .premium-badge { position: absolute; top: 0; right: 0; background: gold; color: #000; padding: 4px 12px; font-size: 10px; font-weight: bold; border-bottom-left-radius: 10px; }
        .price-box { color: #FFD700; font-size: 12px; font-weight: bold; margin-top: 5px; display: block; border: 1px solid #FFD700; padding: 2px 8px; border-radius: 4px; width: fit-content; }
        
        .btn-download { background: #00FFFF; color: #000; padding: 6px 12px; border-radius: 5px; font-size: 11px; font-weight: bold; margin-left: 10px; }
        .btn-locked { background: #333; color: #777; padding: 6px 12px; border-radius: 5px; font-size: 11px; font-weight: bold; cursor: not-allowed; }
    </style>
</head>
<body>

<div class="wrapper">
    <div class="title">🎬 <?php echo htmlspecialchars($data['title'] ?? 'Download List'); ?></div>

    <div class="grid">
        <?php
        if($data && isset($data['medias'])){
            foreach($data['medias'] as $media){
                $q_name = $media['quality'];
                $type = $media['type']; 
                $ext = $media['extension'];
                
                // ডাটাবেস সেটিংস চেক (Case-insensitive)
                $key = strtolower($q_name);
                $setting = isset($q_settings[$key]) ? $q_settings[$key] : ['is_premium'=>0, 'is_hidden'=>0, 'price'=>0];

                if($setting['is_hidden']) continue;

                $card_class = ($type == 'audio') ? 'audio-card' : 'video-card';
                $icon = ($type == 'audio') ? '🎵' : '📹';
                
                if($setting['is_premium'] == 1){
                    // প্রিমিয়াম ভিউ
                    echo "
                    <div class='card $card_class' style='opacity: 0.8;'>
                        <div class='premium-badge'>👑 PREMIUM</div>
                        <div class='icon'>$icon</div>
                        <div class='info'>
                            <span>$type ($ext)</span>
                            <b>$q_name</b>
                            <span class='price-box'>Price: $".$setting['price']."</span>
                        </div>
                        <div class='btn-locked'>LOCKED</div>
                    </div>";
                } else {
                    // ফ্রি ভিউ
                    echo "
                    <a href='{$media['url']}' target='_blank' class='card $card_class'>
                        <div class='icon'>$icon</div>
                        <div class='info'>
                            <span>$type ($ext)</span>
                            <b>$q_name</b>
                        </div>
                        <div class='btn-download'>DOWNLOAD</div>
                    </a>";
                }
            }
        }
        ?>
    </div>
    <br><a href="index.php" style="color: #00FFFF; text-decoration:none;">← Back Home</a>
</div>

</body>
</html>