<?php

namespace App;

use Auth;
use DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        "id", "name","display_name", "email", "password", "remember_token", "status", "last_login", "first_name", "last_name", "created_at", "updated_at", "deleted_at", "bio", "gender", "dob", "pic", "country", "state", "city", "address", "postal","consortia","org_name","contact_no","user_type","counter_usage_report","ebook_reviewer_flag","referral_url","referral_hash","referral_token","referral_login",
            "user_associations","counter_report_year","openathensOrganisationId","openathensrealmName","openathensderivedEduPersonScope","proxy_download","ip","counter_report_type","ingenta_id","institution_id"
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    private static $uc_type = array(111=>'trail',222=>'trail',888=>'trail',333=>'corporate',444=>'token',555=>'token',666=>'token',777=>'corporate');

    public function role()
    {
        //return $this->belongsToMany('Role', 'role_users', 'user_id', 'role_id');
        return $this->belongsToMany(Role::class);
        //return $this->hasOne('App\Role', 'id', 'role_id');
    }

    /*
     * Date: 15-08-2018
     * Author: Muhammad Arshad
     * Parameter: null
     * Description:  this function will return the login user info
     */

    public static function getUserInfo()
    {

        $user    = Auth::user();
        $userobj = User::with('role')->find($user->id);
        return $userobj;

    }

    /*
     * Date: 15-08-2018
     * Author: Muhammad Arshad
     * Parameter: null
     * Description:  this function will return the login user module
     */

    public static function getUserModules()
    {

        $user = Auth::user();
        //dd($user); die;

        $modules = DB::table('role_module')
            ->join('modules', 'modules.id', '=', 'role_module.module_id')
            ->join('users', 'users.id', '=', 'role_module.user_id')
            ->where('role_module.user_id', '=', $user->id)
            ->select("modules.name")
            ->get();
        return $modules;
    }

    /*
     * Date: 15-08-2018
     * Author: Muhammad Arshad
     * Parameter: search (optional)
     * Description:  this function will return the user list
     */

    public static function getUserList($search = "")
    {

        if ($search != "") {
            $users = DB::table('users')->where('name', 'like', '%' . $search . '%')->orwhere('email', 'like', '%' . $search . '%')->orwhere('first_name', 'like', '%' . $search . '%')->orwhere('last_name', 'like', '%' . $search . '%')->where('status', 1)->paginate(10);
        } else {
            $users = DB::table('users')->where('status', 1)->paginate(10);
        }
        return $users;
    }

    /*
     * Date: 15-08-2018
     * Author: Muhammad Arshad
     * Parameter: userid
     * Description:  this function will return the user info
     */

    public static function getUserById($id)
    {
        $userobj = User::with('role')->find($id);

        return $userobj;
    }

    public static function getUserByName($user_input)
    {
        $userobj = DB::table('users')->where('name', $user_input)->first();
        return $userobj;
    }

    /*
     * Date: 27-03-2018
     * Author: Imran Khan
     * Parameter: field,user_id
     * Description:  this function will user info
     */

    public static function GetUserByField($field,$data)
    {
        $user_info = DB::table('users')->where($field, $data)->first();
        return $user_info;
    }

    /*
     * Date: 11-01-2020
     * Author: Imran Khan
     * Parameter: referral url
     * Description:  this function will user info
     */

    public static function GetUserReferral($url)
    {
        $user_info = DB::table('users')->where('referral_hash', 'LIKE', '%' . $url . '%')->first();
        return $user_info;
    }

    /*
     * Date: 15-08-2018
     * Author: Muhammad Arshad
     * Parameter: userid
     * Description:  this function will delete the user
     */

    public static function DeleteUser($id)
    {
        DB::table('users')->where('id', $id)->update(['deleted_at' => 'now()', 'status' => 3]);
        return true;
    }

    /*
    * Date: 11-01-2020
    * Author: Imran Khan
    * Parameter: data
    * Description:  this function will Update user info
    */

    public static function UpdateUser($id,$data)
    {
        return DB::table('users')
        ->where('id', $id)
        ->update($data);
    }

    /*
     * Date: 01-10-2021
     * Author: Imran Khan
     * Parameter: data[]
     * Description:  this function will save the user domain
     */
    public static function AddUserDomain($data)
    {
        $id = DB::table('users_domain')->insert($data);
    }

    /*
     * Date: 01-10-2021
     * Author: Imran Khan
     * Parameter: data[]
     * Description:  this function will save the user domain
     */
    public static function DeleteUserDomain($id,$field)
    {
        DB::table('users_domain')->where($field, $id)->delete();

        return $id;
    }

    /*
     * Date: 01-10-2021
     * Author: Imran Khan
     * Parameter: data[]
     * Description:  this function will save the user domain
     */

    public static function AddUserConsortiaCounter($data)
    {
        $id = DB::table('user_consortia_counter')->insert($data);
    }

    /*
     * Date: 01-10-2021
     * Author: Imran Khan
     * Parameter: data[]
     * Description:  this function will save the user domain
     */
    public static function DeleteUserConsortiaCounter($id,$field)
    {
        DB::table('user_consortia_counter')->where($field, $id)->delete();

        return $id;
    }
    /*
     * Date: 07-10-2021
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will return the user domain detail
     */
    public static function getUserDomainById($id, $column)
    {
        $users_domain = DB::table('users_domain')->select('*')->where($column, '=', $id)->get();

        if($users_domain->isEmpty())

        {
            return null;
        }
        else
        {
            return $users_domain;
        }
    }
    /**************************************************************
    *
    *   Author              : Enayat
    *   Date                : 25-03-2022
    *   Description         : This function will return users  agents
    *
    ****************************************************************/
    public static function getUserAgentAccessByName($id)
    {
        $agent_access = DB::table('tbl_agent_access')
        ->join('users','users.id','=','tbl_agent_access.agent_id')
        ->select('tbl_agent_access.*','users.name')->where('tbl_agent_access.user_id', '=', $id)->get();
        if($agent_access->isEmpty())
        {
            return null;
        }
        else
        {
            return $agent_access;
        }

    }
    public static function getUserAgentAccessByCountry($id)
    {
        $agent_access = DB::table('tbl_agent_access')
        ->select('*')->where('user_id', '=', $id)->get();
        if($agent_access->isEmpty())
        {
            return null;
        }
        else
        {
            return $agent_access;
        }

    }
    // ***************************************************************************************************
    /*
     * Date: 07-10-2021
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will return the user domain detail
     */
    public static function getUserConsoriaById($param = '')
    {

        $user_consortia = DB::table('user_consortia_counter as uc')->selectRaw('uc.user_name, u.id, u.name, u.email, u.status, u.first_name, u.last_name, u.display_name, u.institution_id, uao.organization_name, uai.access_base_type, uai.access_type_on')
        ->join('users as u', 'uc.user_id', '=', 'u.id')
        ->join('user_access_organizations as uao', 'u.access_organization_id', '=', 'uao.access_organization_id')
        ->leftjoin('user_access_info as uai', 'u.id', '=', 'uai.user_id');
        
        if (!empty($param['user_access_status'])) {
        $user_consortia = $user_consortia->selectRaw('uai.user_access_id');
        $user_consortia = $user_consortia->where('user_access_status', '=', $param['user_access_status']);
        }

        if (!empty($param['agent_id'])) {
        $user_consortia = $user_consortia->where('agent_id', '=', $param['agent_id']);
        }
        
        if (!empty($param['groupby'])) 
        {
            $user_consortia = $user_consortia->groupBy('u.id');
        }


        $user_consortia = $user_consortia->orderBy('uc.ucc_id', 'DESC');

        if (!empty($param['paginate'])) {

            $user_consortia = $user_consortia->paginate(10);
        }
        else
        {
             $user_consortia = $user_consortia->get();
        }

        return $user_consortia; 
    }

    /*
     * Date: 07-10-2021
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will return the user domain detail
     */
    public static function GetUserDomain($param)
    {
        $domain = DB::table('users_domain as ud')->selectRaw('u.id, ud.user_domain, u.name, u.email, u.status, u.first_name, u.last_name, u.proxy_download')
        ->join('users as u', 'ud.user_id', '=', 'u.id');

        if (!empty($param['name'])) {
            $domain = $domain->where('u.name', '=', $param['name']);
        }

        if (!empty($param['user_domain'])) {
            $domain = $domain->where('ud.user_domain', 'like', '%' . $param['user_domain']);
        }

        if (!empty($param['email'])) {
            $domain = $domain->where('u.email', '=', $param['email']);
        }

        $domain = $domain->orderBy('ud.user_domin_id', 'DESC');

        if (!empty($param['paginate'])) {

            $domain = $domain->paginate(10);
        }
        else
        {
             $domain = $domain->get();
        }

        return $domain;
    }
    /*
     * Date: 07-10-2021
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will return the user details
     */
    public static function GetUsergroupConcat($user_associations)
    {
        $users = DB::table("users")
            ->selectRaw("group_concat(id) as user_associations")
            ->whereRaw("name in ('" . $user_associations . "')")
            ->first();

        return $users;
    }

    /*
     * Date: 04-06-2022
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will return the institute detail
     */
    public static function GetInstitutes($param = '')
    {
        $institute = DB::table('tbl_institutes as i')->selectRaw('i.*');

        if (!empty($param['grid_id'])) {
            $institute = $institute->where('i.grid_id', '=', $param['grid_id']);
        }

        if (!empty($param['institution_id'])) {
            $institute = $institute->where('i.institution_id', '=', $param['institution_id']);
        }

        if (!empty($param['name'])) {
            $institute = $institute->where('i.name', 'like', '%' . $param['name'] . '%');
        }

        if (!empty($param['paginate'])) {

            $institute = $institute->paginate(10);
        }
        else
        {
             $institute = $institute->get();
        }
        return $institute;
    }

     /*
     * Date: 25-08-2022
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will return the user organization name
     */
    public static function GetUserOrganization($id)
    {
        $users = DB::table("users as u")->selectRaw('u.*, o.organization_name')
            ->join('user_access_organizations as o', 'u.access_organization_id', '=', 'o.access_organization_id')
            ->where('u.id', '=', $id)
            ->first();

        return $users;
    }

    /*
     * Date: 31-08-2022
     * Author: Imran
     * Parameter: agent_id
     * Description:  this function will return the agent members if exist
     */
    public static function GetMemberAdmin($agent_id)
    {
        $sql = "select * from user_consortia_counter where user_id = ".$agent_id;

           $result = DB::select($sql);    
           // echo $sql; die;
            if($result) 
                return false;
            else
                return true;
    }  

}
