<?php
defined('_ISVALID') or exit;

class VHomepageEnhanced {
    public static function getHomepageData() {
        global $class_database;
        
        $data = [];
        
        // Get latest videos
        $data['latest_videos'] = self::getLatestVideos(12);
        
        // Get trending videos (most viewed in last 7 days)
        $data['trending_videos'] = self::getTrendingVideos(8);
        
        // Get featured videos
        $data['featured_videos'] = self::getFeaturedVideos(6);
        
        // Get platform stats
        $data['stats'] = self::getPlatformStats();
        
        // Get categories
        $data['categories'] = self::getCategories();
        
        return $data;
    }
    
    public static function getLatestVideos($limit = 12) {
        global $class_database;
        
        $sql = "SELECT vf.*, au.usr_user as username, au.usr_key as user_key
                FROM db_videofiles vf 
                LEFT JOIN db_accountuser au ON vf.usr_id = au.usr_id 
                WHERE vf.privacy = 'public' AND vf.approved = 1 AND vf.usr_id > 0
                ORDER BY vf.upload_date DESC 
                LIMIT ?";
                
        return $class_database->execute($sql, [$limit]);
    }
    
    public static function getTrendingVideos($limit = 8) {
        global $class_database;
        
        $sql = "SELECT vf.*, au.usr_user as username, au.usr_key as user_key
                FROM db_videofiles vf 
                LEFT JOIN db_accountuser au ON vf.usr_id = au.usr_id 
                WHERE vf.privacy = 'public' AND vf.approved = 1 
                AND vf.upload_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
                ORDER BY vf.file_views DESC, vf.upload_date DESC
                LIMIT ?";
                
        return $class_database->execute($sql, [$limit]);
    }
    
    public static function getFeaturedVideos($limit = 6) {
        global $class_database;
        
        $sql = "SELECT vf.*, au.usr_user as username, au.usr_key as user_key
                FROM db_videofiles vf 
                LEFT JOIN db_accountuser au ON vf.usr_id = au.usr_id 
                WHERE vf.privacy = 'public' AND vf.approved = 1 
                AND vf.featured = 1
                ORDER BY vf.upload_date DESC
                LIMIT ?";
                
        return $class_database->execute($sql, [$limit]);
    }
    
    public static function getPlatformStats() {
        global $class_database;
        
        $stats = [];
        
        // Total videos
        $result = $class_database->execute("SELECT COUNT(*) as count FROM db_videofiles WHERE privacy = 'public' AND approved = 1");
        $stats['total_videos'] = number_format($result[0]['count'] ?? 0);
        
        // Total views
        $result = $class_database->execute("SELECT SUM(file_views) as total FROM db_videofiles WHERE privacy = 'public'");
        $stats['total_views'] = number_format($result[0]['total'] ?? 0);
        
        // Total users
        $result = $class_database->execute("SELECT COUNT(*) as count FROM db_accountuser WHERE usr_active = 1");
        $stats['total_users'] = number_format($result[0]['count'] ?? 0);
        
        // Videos uploaded today
        $result = $class_database->execute("SELECT COUNT(*) as count FROM db_videofiles WHERE DATE(upload_date) = CURDATE()");
        $stats['videos_today'] = $result[0]['count'] ?? 0;
        
        return $stats;
    }
    
    public static function getCategories() {
        global $class_database;
        
        $sql = "SELECT c.*, COUNT(vf.file_id) as video_count 
                FROM db_categories c 
                LEFT JOIN db_videofiles vf ON c.ct_id = vf.file_category AND vf.privacy = 'public' AND vf.approved = 1
                WHERE c.ct_active = 1 
                GROUP BY c.ct_id 
                ORDER BY video_count DESC, c.ct_name ASC";
                
        return $class_database->execute($sql);
    }
    
    public static function renderVideoCard($video, $size = 'medium') {
        $thumb = $video['file_thumbnail'] ?: '/f_scripts/fe/img/default-thumb.jpg';
        $title = htmlspecialchars($video['file_title']);
        $username = htmlspecialchars($video['username'] ?: 'Unknown');
        $duration = $video['file_duration'] ?: '00:00';
        $views = number_format($video['file_views'] ?: 0);
        $upload_date = date('M j, Y', strtotime($video['upload_date']));
        
        $card_class = $size === 'large' ? 'video-card-large' : 'video-card';
        
        return "
        <div class=\"$card_class\">
            <div class=\"video-thumbnail\">
                <a href=\"/watch/{$video['file_key']}\">
                    <img src=\"$thumb\" alt=\"$title\" loading=\"lazy\">
                    <span class=\"duration\">$duration</span>
                    <div class=\"play-overlay\">
                        <i class=\"icon-play\"></i>
                    </div>
                </a>
            </div>
            <div class=\"video-details\">
                <h3 class=\"video-title\">
                    <a href=\"/watch/{$video['file_key']}\">$title</a>
                </h3>
                <div class=\"video-meta\">
                    <a href=\"/user/{$video['user_key']}\" class=\"username\">$username</a>
                    <span class=\"views\">$views views</span>
                    <span class=\"date\">$upload_date</span>
                </div>
            </div>
        </div>";
    }
}
?>