<?php

namespace Lycoreco\Apps\Index\Controllers;

use Lycoreco\Apps\Recipes\Models\CategoryModel;
use Lycoreco\Apps\Recipes\Models\RecipeModel;
use Lycoreco\Apps\Recipes\Models\RecipeUserMenu;
use Lycoreco\Apps\Recipes\Models\ReviewsModel;
use Lycoreco\Includes\BaseController;

require_once(INCLUDES_PATH . '/Const/recipes.php');

class HomepageController extends BaseController
{
    protected $template_name = APPS_PATH . '/Index/Templates/index.php';

    public function get_context_data() {
        $context = parent::get_context_data();

        $context['latest_recipes'] = RecipeModel::filter(array(
            [
                'name'      => 'obj.status',
                'type'      => '=',
                'value'     => 'publish'
            ]),
            ['-obj.created_at'],
            3
        );
        $context['categories'] = CategoryModel::filter();
        $dayNumber = date("w");
        $dayofweek = DAYS_OF_WEEK[$dayNumber];

        $context['reviews'] = ReviewsModel::filter(array(
            [
                'name'      => 'obj.status',
                'type'      => '=',
                'value'     => 'publish'
            ]),
            ['-obj.created_at'],
            6
        );

        if(CURRENT_USER) {
            $context['usermenu_recipe_prefetch'] = RecipeUserMenu::get_prefetch_recipes(CURRENT_USER, $dayofweek);
        }
        else {
            $context['usermenu_recipe_prefetch'] = [ ];
        }

        return $context;
    }
}
