<?php
if (!defined('_ISVALID')) define('_ISVALID', true);
include_once 'f_core/config.core.php';

$search_query = $_GET['q'] ?? '';
$videos = [];

if ($search_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 ? OR vf.file_description LIKE ?) 
            AND vf.privacy = 'public' AND vf.approved = 1 
            ORDER BY vf.upload_date DESC 
            LIMIT 20";
    
    $search_term = '%' . $search_query . '%';
    $videos = $class_database->execute($sql, [$search_term, $search_term]);
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Search Videos - EasyStream</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; padding: 20px; background: #f8f9fa; }
        .container { max-width: 1200px; margin: 0 auto; }
        .header { text-align: center; margin-bottom: 40px; }
        .logo img { max-height: 50px; }
        .search-form { max-width: 600px; margin: 0 auto 40px auto; }
        .search-input { width: 100%; padding: 15px; border: 1px solid #ddd; border-radius: 25px; font-size: 16px; }
        .search-btn { padding: 15px 30px; background: #007bff; color: white; border: none; border-radius: 25px; margin-left: 10px; cursor: pointer; }
        .video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
        .video-card { background: white; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
        .video-thumb { width: 100%; height: 180px; background: #e9ecef; display: flex; align-items: center; justify-content: center; color: #6c757d; }
        .video-info { padding: 15px; }
        .video-title { margin: 0 0 8px 0; font-size: 16px; font-weight: 500; }
        .video-meta { color: #666; font-size: 14px; }
        .nav-links { text-align: center; margin-bottom: 30px; }
        .nav-links a { margin: 0 15px; color: #007bff; text-decoration: none; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <img src="/f_scripts/fe/img/logo-header-blue.svg" alt="EasyStream">
            <h1>Search Videos</h1>
        </div>
        
        <form class="search-form" method="get">
            <input type="text" name="q" class="search-input" value="<?php echo htmlspecialchars($search_query); ?>" placeholder="Search for videos...">
            <button type="submit" class="search-btn">Search</button>
        </form>
        
        <div class="nav-links">
            <a href="/">← Home</a>
            <a href="browse.php">Browse All</a>
            <a href="upload.php">Upload Video</a>
        </div>
        
        <?php if ($search_query): ?>
            <h2>Search Results for "<?php echo htmlspecialchars($search_query); ?>"</h2>
        <?php endif; ?>
        
        <div class="video-grid">
            <?php if ($videos && count($videos) > 0): ?>
                <?php foreach ($videos as $video): ?>
                    <div class="video-card">
                        <div class="video-thumb">
                            📹 Video Thumbnail
                        </div>
                        <div class="video-info">
                            <h3 class="video-title"><?php echo htmlspecialchars($video['file_title']); ?></h3>
                            <div class="video-meta">
                                By: <?php echo htmlspecialchars($video['username'] ?: 'Unknown'); ?><br>
                                Views: <?php echo number_format($video['file_views'] ?: 0); ?><br>
                                Duration: <?php echo gmdate("H:i:s", $video['file_duration'] ?: 0); ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php elseif ($search_query): ?>
                <div style="grid-column: 1 / -1; text-align: center; padding: 40px;">
                    <h3>No videos found</h3>
                    <p>Try different search terms or <a href="browse.php">browse all videos</a></p>
                </div>
            <?php else: ?>
                <div style="grid-column: 1 / -1; text-align: center; padding: 40px;">
                    <h3>Search for Videos</h3>
                    <p>Enter keywords to find videos you're looking for</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>