<?php
/*******************************************************************************************************************
| Software Name        : EasyStream
| Software Description : High End YouTube Clone Script with Videos, Shorts, Streams, Images, Audio, Documents, Blogs
| Software Author      : (c) Sami Ahmed
|*******************************************************************************************************************
|
|*******************************************************************************************************************
| This source file is subject to the EasyStream Proprietary License Agreement.
|
| By using this software, you acknowledge having read this Agreement and agree to be bound thereby.
|*******************************************************************************************************************
| Copyright (c) 2025 Sami Ahmed. All rights reserved.
|*******************************************************************************************************************/

/**
 * Personalized Homepage with Recommendations
 *
 * This is an enhanced version of index.php that includes:
 * - Personalized "For You" feed
 * - Trending content section
 * - Continue watching section
 * - Subscriptions feed
 * - Dynamic content recommendations
 *
 * To use this as your homepage, rename index.php to index_backup.php
 * and rename this file to index.php
 */

define('_ISVALID', true);

include_once 'f_core/config.core.php';

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

// Initialize sidebar as visible for new users if not set
if (!isset($_SESSION['sbm'])) {
    $_SESSION['sbm'] = 1;
}

// Load language files
include_once $class_language->setLanguageFile('frontend', 'language.home');
include_once $class_language->setLanguageFile('frontend', 'language.global');
include_once $class_language->setLanguageFile('frontend', 'language.files');

// Get user ID
$usr_id = isset($_SESSION['USER_ID']) ? (int) $_SESSION['USER_ID'] : 0;
$is_logged_in = $usr_id > 0;

// Get configuration
$cfg = $class_database->getConfigurations('video_module,short_module,live_module,image_module,audio_module,document_module,blog_module,user_subscriptions,file_history');

// Prepare content sections
$sections = [];

// 1. Continue Watching (only for logged-in users)
if ($is_logged_in && $cfg['file_history'] == 1) {
    $continue_watching = VRecommendations::getContinueWatching($usr_id, 10);
    if (!empty($continue_watching)) {
        $sections['continue_watching'] = [
            'title' => 'Continue Watching',
            'items' => $continue_watching,
            'type' => 'mixed'
        ];
    }
}

// 2. For You / Recommended Videos (personalized for logged-in users, trending for guests)
if ($cfg['video_module'] == 1) {
    $for_you = VRecommendations::getForYouFeed(20, 'video');
    $sections['for_you'] = [
        'title' => $is_logged_in ? 'Recommended for You' : 'Trending Videos',
        'items' => $for_you,
        'type' => 'video'
    ];
}

// 3. Subscriptions Feed (only for logged-in users)
if ($is_logged_in && $cfg['user_subscriptions'] == 1 && $cfg['video_module'] == 1) {
    $subscriptions = VRecommendations::getFromSubscriptions($usr_id, 15, 'video');
    if (!empty($subscriptions)) {
        $sections['subscriptions'] = [
            'title' => 'Latest from Subscriptions',
            'items' => $subscriptions,
            'type' => 'video'
        ];
    }
}

// 4. Trending Shorts
if ($cfg['short_module'] == 1) {
    $trending_shorts = VRecommendations::getTrending(15, 'short');
    $sections['trending_shorts'] = [
        'title' => 'Trending Shorts',
        'items' => $trending_shorts,
        'type' => 'short'
    ];
}

// 5. Live Streams
if ($cfg['live_module'] == 1) {
    $live_streams = VRecommendations::getTrending(10, 'live');
    $sections['live'] = [
        'title' => 'Live Now',
        'items' => $live_streams,
        'type' => 'live'
    ];
}

// 6. Popular This Week
if ($cfg['video_module'] == 1) {
    $popular_week = VRecommendations::getTrending(20, 'video');
    $sections['popular_week'] = [
        'title' => 'Popular This Week',
        'items' => $popular_week,
        'type' => 'video'
    ];
}

// Assign sections to Smarty
$smarty->assign('personalized_sections', $sections);
$smarty->assign('is_logged_in', $is_logged_in);
$smarty->assign('usr_id', $usr_id);

// Display the enhanced homepage
echo $class_smarty->displayPage('frontend', 'tpl_index_personalized', null, null);
?>
