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

class VVideoBrowse {
    public static function getLatestVideos($limit = 20, $page = 1) {
        global $class_database;
        
        $offset = ($page - 1) * $limit;
        $sql = "SELECT vf.*, au.usr_user as username 
                FROM db_videofiles vf 
                LEFT JOIN db_accountuser au ON vf.usr_id = au.usr_id 
                WHERE vf.privacy = 'public' AND vf.approved = 1 
                ORDER BY vf.upload_date DESC 
                LIMIT $limit OFFSET $offset";
                
        return $class_database->execute($sql);
    }
    
    public static function searchVideos($query, $limit = 20) {
        global $class_database;
        
        $query = $class_database->escape($query);
        $sql = "SELECT vf.*, au.usr_user as username 
                FROM db_videofiles vf 
                LEFT JOIN db_accountuser au ON vf.usr_id = au.usr_id 
                WHERE (vf.file_title LIKE '%$query%' OR vf.file_description LIKE '%$query%') 
                AND vf.privacy = 'public' AND vf.approved = 1 
                ORDER BY vf.upload_date DESC 
                LIMIT $limit";
                
        return $class_database->execute($sql);
    }
    
    public static function getVideosByCategory($category_id, $limit = 20) {
        global $class_database;
        
        $sql = "SELECT vf.*, au.usr_user as username 
                FROM db_videofiles vf 
                LEFT JOIN db_accountuser au ON vf.usr_id = au.usr_id 
                WHERE vf.file_category = ? AND vf.privacy = 'public' AND vf.approved = 1 
                ORDER BY vf.upload_date DESC 
                LIMIT $limit";
                
        return $class_database->execute($sql, [$category_id]);
    }
    
    public static function renderVideoGrid($videos) {
        if (empty($videos)) {
            return '<div class="no-videos">No videos found</div>';
        }
        
        $html = '<div class="video-grid">';
        foreach ($videos as $video) {
            $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);
            
            $html .= "
            <div class=\"video-item\">
                <div class=\"video-thumb\">
                    <img src=\"$thumb\" alt=\"$title\">
                    <span class=\"duration\">$duration</span>
                </div>
                <div class=\"video-info\">
                    <h3><a href=\"/watch/{$video['file_key']}\">{$title}</a></h3>
                    <p class=\"video-meta\">
                        <span class=\"username\">$username</span>
                        <span class=\"views\">$views views</span>
                    </p>
                </div>
            </div>";
        }
        $html .= '</div>';
        
        return $html;
    }
}
?>