<?php
namespace Lycoreco\Apps\Admin\Controllers;

use Lycoreco\Apps\Recipes\Models\IngredientModel;
use Lycoreco\Apps\Recipes\Models\RecipeModel;
use Lycoreco\Apps\Users\Models\BanlistModel;
use Lycoreco\Apps\Users\Models\UserModel;

class AdminHomeController extends Abstract\AdminBaseController
{
    protected $template_name = APPS_PATH . '/Admin/Templates/home.php';

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

        $context['last_orders'] = [];

        $datetime_month_ago = new \DateTime();
        $datetime_month_ago->modify("-1 month");
        $datetime_month_ago_text = $datetime_month_ago->format('Y-m-d H:i:s' . '\'');

        $context['user_count'] = UserModel::count(array(
            [
                'name'      =>      'obj.register_at',
                'type'      =>      '>=',
                'value'     =>      $datetime_month_ago_text
            ]
        ));
        $context['recipes_count'] = RecipeModel::count(array(
            [
                'name'      =>      'obj.created_at',
                'type'      =>      '>=',
                'value'     =>      $datetime_month_ago_text
            ]
        ));
        $context['ban_count'] = BanlistModel::count(array(
            [
                'name'      =>      'obj.created_at',
                'type'      =>      '>=',
                'value'     =>      $datetime_month_ago_text
            ]
        ));
        $context['latest_recipes'] = RecipeModel::filter(
            array(), 
            ['-obj.created_at']
        );


        return $context;
    }
}
