<?php
namespace App\Traits;

use App\Models\Company;

trait QuotaRolesCompany {

    public $counter_administrators_plan, $counter_administrative_plan, $counter_employees_plan;
    public $counter_administrators_company, $counter_administrative_company, $counter_employees_company;
    public $roles_all, $roles_company_all, $roles_company_administrative, $show_btn_add, $company_id;

    public function setCompanyId($company_id) {
        $this->company_id = $company_id;
    }
    public function counters_roles()//obtiene los valores de los roles de la empresa y el plan que tiene asignado
    {
        $this->roles_company_administrative = roles_user(['Soporte', 'Super Admin', 'Administrador', 'Empleado']);
        $this->counter_administrators_plan = Company::find($this->company_id)
                                ->join('plans', 'plans.id', '=', 'companies.plan_id')->get()->first()->max_users_administrators;
        $this->counter_administrative_plan = Company::find($this->company_id)
                                ->join('plans', 'plans.id', '=', 'companies.plan_id')->get()->first()->max_users_administrative;
        $this->counter_employees_plan = Company::find($this->company_id)
                                ->join('plans', 'plans.id', '=', 'companies.plan_id')->get()->first()->max_users_employees;

        $this->counter_administrators_company = Company::find($this->company_id)->users()->role('Administrador')->count();
        $this->counter_administrative_company = Company::find($this->company_id)->users()->role($this->roles_company_administrative)->count();
        $this->counter_employees_company = Company::find($this->company_id)->users()->role('Empleado')->count();

        $this->show_btn_add = ($this->counter_administrators_plan - $this->counter_administrators_company) + ($this->counter_administrative_plan - $this->counter_administrative_company) + ($this->counter_employees_plan - $this->counter_employees_company);
    }

    public function roles_company_allowed()
    {
        $roles_not_allowed=['Super Admin', 'Soporte'];
        $this->counters_roles();

        if ($this->counter_administrators_plan <= $this->counter_administrators_company)
        {
            $roles_not_allowed[] = 'Administrador';
        }
        if ($this->counter_administrative_plan <= $this->counter_administrative_company)
        {
            $roles_not_allowed[] = 'Recursos Humanos';
            $roles_not_allowed[] = 'Asistente Administrativo';
        }
        if ($this->counter_employees_plan <= $this->counter_employees_company)
        {
            $roles_not_allowed[] = 'Empleado';
        }

        $this->roles_company_all = roles_user($roles_not_allowed);
    }

    public function role_quota_max($role){
        foreach ($this->roles_company_administrative as $role_company){
            if ($role_company == $role)
            {
                $role = 'Administrativo'; // Cambiar el rol a administrativo para compararlo con los permisos
            }
        }
        $quota=0;
        //role = array('0' => 'Administrador') or array('0' => 'Administrativo') or array('0' => 'Empleado')
        //para comparar con los permisos se debe convertir a string de la siguiente manera: $role[0]
        switch ($role[0]) {
            case 'Administrador':
                $quota = ($this->counter_administrators_plan - $this->counter_administrators_company);
                break;
            case 'Administrativo':
                $quota = ($this->counter_administrative_plan - $this->counter_administrative_company);
                break;
            case 'Empleado':
                $quota = ($this->counter_employees_plan - $this->counter_employees_company);
                break;
        }

        return $quota;
    }

}
