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

// Check if user is logged in
if (!isset($_SESSION['usr_id'])) {
    header('Location: signin.php');
    exit;
}

// Handle file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = VSecurity::postParam('title', 'string');
    $description = VSecurity::postParam('description', 'string');
    
    if ($title && isset($_FILES['video_file']) && $_FILES['video_file']['error'] === UPLOAD_ERR_OK) {
        $file = $_FILES['video_file'];
        
        // Basic validation
        $allowed_types = ['video/mp4', 'video/avi', 'video/quicktime'];
        if (in_array($file['type'], $allowed_types) && $file['size'] <= 100 * 1024 * 1024) { // 100MB max
            
            $file_key = rand(100000, 999999);
            $upload_dir = 'f_data/videos/';
            
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0755, true);
            }
            
            $filename = $file_key . '_' . basename($file['name']);
            $target_path = $upload_dir . $filename;
            
            if (move_uploaded_file($file['tmp_name'], $target_path)) {
                // Save to database
                $sql = "INSERT INTO db_videofiles (usr_id, file_key, file_type, file_title, file_description, 
                        file_name, privacy, upload_date, approved, file_views) 
                        VALUES (?, ?, 'video', ?, ?, ?, 'public', NOW(), 1, 0)";
                
                $result = $class_database->execute($sql, [
                    $_SESSION['usr_id'], $file_key, $title, $description, $filename
                ]);
                
                if ($result) {
                    $success = 'Video uploaded successfully!';
                } else {
                    $error = 'Database error occurred';
                }
            } else {
                $error = 'Failed to upload file';
            }
        } else {
            $error = 'Invalid file type or size too large (max 100MB)';
        }
    } else {
        $error = 'Please fill in all required fields';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Upload Video - EasyStream</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; padding: 20px; background: #f8f9fa; }
        .container { max-width: 600px; margin: 50px auto; }
        .upload-card { background: white; padding: 40px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
        .logo { text-align: center; margin-bottom: 30px; }
        .logo img { max-height: 50px; }
        h1 { text-align: center; margin-bottom: 30px; color: #333; }
        .form-group { margin-bottom: 20px; }
        label { display: block; margin-bottom: 5px; font-weight: 500; }
        input[type="text"], input[type="file"], textarea { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; box-sizing: border-box; }
        textarea { height: 100px; resize: vertical; }
        .btn { width: 100%; padding: 12px; background: #007bff; color: white; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; }
        .btn:hover { background: #0056b3; }
        .error { background: #f8d7da; color: #721c24; padding: 10px; border-radius: 6px; margin-bottom: 20px; }
        .success { background: #d4edda; color: #155724; padding: 10px; border-radius: 6px; margin-bottom: 20px; }
        .links { text-align: center; margin-top: 20px; }
        .links a { color: #007bff; text-decoration: none; margin: 0 10px; }
        small { color: #666; font-size: 12px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="upload-card">
            <div class="logo">
                <img src="/f_scripts/fe/img/logo-header-blue.svg" alt="EasyStream">
            </div>
            <h1>📤 Upload Video</h1>
            
            <?php if (isset($success)): ?>
                <div class="success"><?php echo htmlspecialchars($success); ?></div>
            <?php endif; ?>
            
            <?php if (isset($error)): ?>
                <div class="error"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <form method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label>📁 Video File</label>
                    <input type="file" name="video_file" accept="video/*" required>
                    <small>Supported: MP4, AVI, MOV (Max: 100MB)</small>
                </div>
                <div class="form-group">
                    <label>📝 Title</label>
                    <input type="text" name="title" value="<?php echo htmlspecialchars($_POST['title'] ?? ''); ?>" required>
                </div>
                <div class="form-group">
                    <label>📄 Description</label>
                    <textarea name="description"><?php echo htmlspecialchars($_POST['description'] ?? ''); ?></textarea>
                </div>
                <button type="submit" class="btn">🚀 Upload Video</button>
            </form>
            
            <div class="links">
                <a href="/">🏠 Home</a>
                <a href="browse.php">📺 Browse Videos</a>
            </div>
        </div>
    </div>
</body>
</html>