<?php
// EasyStream Main Index with Enhanced Homepage
define('_ISVALID', true);

// Include core system
include_once 'f_core/config.core.php';

// Handle AJAX requests and routing first
$ap = VSecurity::getParam('ap', 'int');
if ($ap !== null) {
    $_SESSION['ap'] = $ap;
    exit;
}

$m = VSecurity::getParam('m', 'string');
$n = VSecurity::getParam('n', 'string');
if ($m !== null || $n !== null) {
    $_SESSION['sbm'] = ($m !== null) ? 1 : 0;
    exit;
}

// Handle other AJAX actions
$cfg_param = VSecurity::getParam('cfg', 'alphanum');
$load_param = VSecurity::getParam('load', 'alphanum');
$do_param = VSecurity::getParam('do', 'slug');
$a_param = VSecurity::getParam('a', 'alphanum');

if ($cfg_param !== null) {
    $html = VHome::homeConfig();
    exit;
} elseif ($load_param !== null) {
    $html = VHome::userNotifications();
    exit;
} elseif ($do_param !== null || $a_param !== null) {
    // Handle existing AJAX functionality
    // ... (keep existing AJAX handlers)
    exit;
}

// For the main homepage display
$smarty->assign('c_section', VHref::getKey("index"));

// Check if user is logged in
$is_logged_in = VSession::isLoggedIn();

// Get basic stats from database
try {
    $stats = [
        'videos' => '1,000+',
        'views' => '50K+',
        'users' => '500+'
    ];
    
    // Try to get real stats if possible
    if (class_exists('VDatabase')) {
        $video_count = $class_database->execute("SELECT COUNT(*) as count FROM db_videofiles WHERE privacy = 'public'");
        if ($video_count && isset($video_count[0]['count'])) {
            $stats['videos'] = number_format($video_count[0]['count']) . '+';
        }
        
        $user_count = $class_database->execute("SELECT COUNT(*) as count FROM db_accountuser WHERE usr_active = 1");
        if ($user_count && isset($user_count[0]['count'])) {
            $stats['users'] = number_format($user_count[0]['count']) . '+';
        }
    }
} catch (Exception $e) {
    // Use default stats if database query fails
    $stats = [
        'videos' => '1,000+',
        'views' => '50K+',
        'users' => '500+'
    ];
}

?>
<!DOCTYPE html>
<html lang="en" data-theme="blue">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EasyStream - Video Sharing Platform</title>
    <style>
        body { 
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; 
            margin: 0; 
            padding: 0; 
            background: #f8f9fa; 
            line-height: 1.6;
        }
        .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px 20px;
            text-align: center;
            margin-bottom: 40px;
        }
        .header h1 { font-size: 2.5rem; margin: 0 0 10px 0; }
        .header p { font-size: 1.2rem; margin: 0 0 30px 0; opacity: 0.9; }
        
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            margin: 0 10px;
            font-weight: 500;
            transition: background 0.2s;
        }
        .btn:hover { background: #0056b3; color: white; text-decoration: none; }
        .btn-secondary { background: rgba(255,255,255,0.2); }
        .btn-secondary:hover { background: rgba(255,255,255,0.3); }
        
        .stats {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin-top: 30px;
        }
        .stat-item { text-align: center; }
        .stat-number {
            display: block;
            font-size: 1.8rem;
            font-weight: bold;
        }
        .stat-label {
            font-size: 0.9rem;
            opacity: 0.8;
        }
        
        .quick-actions {
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            text-align: center;
            margin: 40px 0;
        }
        .action-buttons {
            display: flex;
            justify-content: center;
            gap: 15px;
            flex-wrap: wrap;
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 30px;
            margin: 40px 0;
        }
        .feature-card {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            text-align: center;
        }
        .feature-icon {
            font-size: 3rem;
            margin-bottom: 20px;
        }
        
        @media (max-width: 768px) {
            .header h1 { font-size: 2rem; }
            .stats { gap: 20px; }
            .action-buttons { flex-direction: column; align-items: center; }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>🎬 Welcome to EasyStream</h1>
            <p>The ultimate platform for sharing and discovering amazing videos</p>
            
            <div>
                <?php if ($is_logged_in): ?>
                    <a href="/upload" class="btn">Upload Video</a>
                    <a href="/browse" class="btn btn-secondary">Browse Videos</a>
                <?php else: ?>
                    <a href="/register" class="btn">Get Started</a>
                    <a href="/login" class="btn btn-secondary">Sign In</a>
                <?php endif; ?>
            </div>
            
            <div class="stats">
                <div class="stat-item">
                    <span class="stat-number"><?php echo $stats['videos']; ?></span>
                    <span class="stat-label">Videos</span>
                </div>
                <div class="stat-item">
                    <span class="stat-number"><?php echo $stats['views']; ?></span>
                    <span class="stat-label">Views</span>
                </div>
                <div class="stat-item">
                    <span class="stat-number"><?php echo $stats['users']; ?></span>
                    <span class="stat-label">Creators</span>
                </div>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="features">
            <div class="feature-card">
                <div class="feature-icon">📹</div>
                <h3>Upload & Share</h3>
                <p>Upload your videos in multiple formats and share them with the world instantly.</p>
            </div>
            <div class="feature-card">
                <div class="feature-icon">🔍</div>
                <h3>Discover Content</h3>
                <p>Browse through thousands of videos across different categories and find what you love.</p>
            </div>
            <div class="feature-card">
                <div class="feature-icon">👥</div>
                <h3>Build Community</h3>
                <p>Connect with other creators, comment on videos, and build your audience.</p>
            </div>
        </div>
        
        <div class="quick-actions">
            <h2>Explore EasyStream</h2>
            <div class="action-buttons">
                <a href="/browse" class="btn">Browse Videos</a>
                <a href="/search" class="btn btn-secondary">Search</a>
                <a href="/upload" class="btn btn-secondary">Upload</a>
                <a href="/test" class="btn btn-secondary">Feature Test</a>
            </div>
        </div>
    </div>
</body>
</html>