<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\Traits\Modules as RemoteModules;
use Route;
use App\Models\Module\Module;

class Suggestions
{
    use RemoteModules;

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        // No need to add suggestions in console
        if (app()->runningInConsole() || !config('app.installed')) {
            return;
        }

        $modules = false;

        if (user()) {
            $path = Route::current()->uri();

            if ($path) {
                $suggestions = $this->getSuggestions($path);

                if ($suggestions) {
                    $suggestion_modules = $suggestions->modules;

                    foreach ($suggestion_modules as $key => $module) {
                        $installed = Module::where('company_id', session('company_id'))->where('alias', $module->alias)->first();

                        if ($installed) {
                            continue;
                        }

                        $modules[] = $module;
                    }
                }
            }
        }

        $view->with(['suggestion_modules' => $modules]);
    }
}
