<?php namespace App\Libs\Composers;

use App\Models\BlogCategory;
use App\Models\BlogPost;
use App\Models\WebsiteObject;

class BlogHomeComposer
{
    public function compose($view)
    {
        $redirectPath = lang_url(config('path.frontend.blog_listing_slug'));

        $featuredPosts = BlogPost::full()
            ->with(['media', 'authors'])
            ->where('status', 'published')
            ->where('lang_status', 'published')
            ->where('is_feature_post', '1')
            ->orderBy('date', 'desc')
            ->limit(2)
            ->get();

        $postsQuery = BlogPost::full()
            ->with(['media', 'authors'])
            ->where('status', 'published')
            ->where('lang_status', 'published')
            ->whereNotIn('id', $featuredPosts->pluck('id'))
            ->orderBy('date', 'desc')
            ->limit(4)
            ->get();

        $categories = BlogCategory::full()
            ->with('media')
            ->where('status', 'published')
            ->where('in_filter', 1)
            ->orderBy('website_objects.position', 'asc')
            ->get();

        $view->with(array(
            'categories' => $categories,
            'featuredPosts' => $featuredPosts,
            'posts' => $postsQuery,
            'redirectPath' => $redirectPath,
        ));
    }
}
