<?php

namespace App\Http\Controllers;

use App\Models\AppSession;
use App\Models\Note;
use App\Models\NoteTemplate;
use App\Models\Pro;
use App\Models\ProProAccess;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Cookie;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $performer = null;

    protected $pro = null;

    protected $pros = null;

    public function __construct()
    {
        $this->performer = get_current_session();
        if($this->performer && $this->performer->pro) {
            $this->pro = $this->performer->pro;
            view()->share('pro', $this->performer->pro);
        }
        view()->share('performer', $this->performer);

        $this->pros = [];

        /*$this->pros =  Pro::all();
        if($this->pro && $this->pro->pro_type != 'ADMIN'){
            $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
            $accessibleProIds = [];
            foreach($accessiblePros as $accessiblePro){
                $accessibleProIds[] = $accessiblePro->id;
            }
            $accessibleProIds[] = $this->pro->id;

            $this->pros = Pro::whereIn('id', $accessibleProIds)->get();
        }*/

        view()->share('pros',$this->pros);
        view()->share('notes', Note::all());

        $noteTemplates = NoteTemplate::all();
        view()->share('noteTemplates', $noteTemplates);

        $initiatives = ['LHI', 'BHI'];
        view()->share('intiatives', $initiatives);

        $rmCodes = ['RMB','RM20', 'RM40','RM60'];
        
        view()->share('rmCodes', $rmCodes);
    }

    public function performer(){
        $sessionKey = Cookie::get('sessionKey');
        if ($sessionKey == null){
            throw new \Exception('No session key in cookie.');
        }
        $performer = AppSession::where('session_key', $sessionKey)->first();
        return $performer;
    }
}
