<?php

namespace App\Models;

class PermissionGroupUserMapping extends BaseModel
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'permission_group_user_mapping';
    protected $appends = [];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    protected $dateFormat = 'U';
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    public function createUser ()
    {
        return $this->belongsTo("App\Models\User","created_by");
    }

    public function property ()
    {
        return $this->belongsTo("App\Models\Property","property_id");
    }

    public function user ()
    {
        return $this->belongsTo("App\Models\User","user_id");
    }

    public function permissionGroup ()
    {
        return $this->belongsTo("App\Models\PermissionGroup","permission_group_id");
    }

    public function permissionGroupMapping ()
    {
        return $this->hasMany("\App\Models\PermissionGroupMapping","permission_group_id","permission_group_id");
    }

    public function getRelatedParametersArrayAttribute ()
    {
        try
        {
            return json_decode ( $this->related_parameters,true );
        } catch ( Exception $e )
        {
            return [];
        }
    }

    public function getRelatedParametersEmployeeDepartmentsAttribute ()
    {
        try
        {
            $departments = [];
            $relatedParameters = $this->getRelatedParametersArrayAttribute ();

            if ( !$relatedParameters)
            {
                return null;
            }

            foreach ($relatedParameters["employee_departments"] as $perParameter)
            {
                $departments[] =  $perParameter["department_id"];
            }

            return $departments;
        } catch ( Exception $e )
        {
            return null;
        }
    }

}
