<?php 
namespace App\Models;

use App\Models\BaseModel;

class UsersModel extends BaseModel
{
    const GENDER_MALE   = 1;
    const GENDER_FEMALE = 2;
    const GENDER_OTHER  = 3;

    protected $table      = 'tbl_users';
    protected $primaryKey = 'user_id';

    protected $returnType     = 'array';
    protected $useSoftDeletes = false;

    protected $allowedFields = [
        'user_type_id', 'first_name', 'last_name', 'email_id', 'mobile_number', 
        'address', 'city_id', 'state_id', 'created_by', 'updated_by'
    ];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
   
    protected $validationRules    = [];
    protected $validationMessages = [];
    protected $skipValidation     = false;
    
    public function findUserWithoutSuperAdminList() {
        
        $objQuery = $this->createBuilderObject()->select( 'tbl_users.*, ts.state_name, tc.city_name, tut.user_type_name' )
                                    ->join( 'tbl_user_types tut', 'tut.user_type_id = tbl_users.user_type_id' )
                                    ->join( 'tbl_states ts', 'ts.state_id = tbl_users.state_id' )
                                    ->join( 'tbl_cities tc', 'tc.city_id = tbl_users.city_id' )        
                                    ->where( 'tbl_users.user_type_id !=', UserTypesModel::USER_TYPE_SUPER_ADMIN )
                                    ->where( 'is_deleted', NOT_DELETED )
                                    ->orderBy( 'user_id', 'desc' )
                                    ->get();
        return $objQuery->getResultArray();
    }

    public function findUserWithoutAdminsList() {

        $objQuery = $this->createBuilderObject()->select( 'tbl_users.*, ts.state_name, tc.city_name, tut.user_type_name, tut.user_type_name, tul.username' )
                                    ->join( 'tbl_user_types tut', 'tut.user_type_id = tbl_users.user_type_id' )
                                    ->join( 'tbl_user_login tul', 'tul.user_id = tbl_users.user_id' )
                                    ->join( 'tbl_states ts', 'ts.state_id = tbl_users.state_id' )
                                    ->join( 'tbl_cities tc', 'tc.city_id = tbl_users.city_id' )        
                                    ->whereNotIn( 'tbl_users.user_type_id', UserTypesModel::ADMINS )
                                    ->where( 'tbl_users.is_deleted', NOT_DELETED )
                                    ->orderBy( 'tbl_users.user_id', 'desc' )
                                    ->get();
        return $objQuery->getResultArray();
    }

    public function findAllUsersByUserTypeId( $intUserTypeId ) {

        $strSelect =  "( select CONCAT(first_name, ' ', last_name) from tbl_users tu where tu.user_id = tbl_users.created_by ) as distributor_name";

        $objQuery = $this->createBuilderObject()->select( 'tbl_users.*, ts.state_name, tc.city_name, tut.user_type_name, tudi.imei_number, tudi.config_status, tudi.config_error_code,  tul.username, ' . $strSelect )
                                    ->join( 'tbl_user_types tut', 'tut.user_type_id = tbl_users.user_type_id' )
                                    ->join( 'tbl_user_login tul', 'tul.user_id = tbl_users.user_id' )
                                    ->join( 'tbl_user_device_info tudi', 'tudi.user_id = tbl_users.user_id', 'left' )
                                    ->join( 'tbl_states ts', 'ts.state_id = tbl_users.state_id', 'left' )
                                    ->join( 'tbl_cities tc', 'tc.city_id = tbl_users.city_id', 'left' )        
                                    ->where( 'tbl_users.user_type_id', $intUserTypeId )
                                    ->where( 'tbl_users.is_deleted', NOT_DELETED )
                                    ->orderBy( 'tbl_users.user_id', 'desc' )
                                    ->get();

        return $objQuery->getResultArray();
    }

   
    public function findUserByUserId( $intUserId ) {
        
        $objQuery = $this->createBuilderObject()->select( 'tbl_users.*, tul.username, ts.state_name, tc.city_name, tut.user_type_name' )
                                    ->join( 'tbl_user_types tut', 'tut.user_type_id = tbl_users.user_type_id' )
                                    ->join( 'tbl_user_login tul', 'tul.user_id = tbl_users.user_id' )
                                    ->join( 'tbl_states ts', 'ts.state_id = tbl_users.state_id', 'left' )
                                    ->join( 'tbl_cities tc', 'tc.city_id = tbl_users.city_id', 'left' )        
                                    ->where( 'tbl_users.user_id', $intUserId )
                                    ->where( 'is_deleted', NOT_DELETED )
                                    ->get();

        return $objQuery->getRowArray();
    }

    public function findUsersByUserIds( $arrintUserIds ) {

        $objQuery = $this->createBuilderObject()->select( 'tbl_users.*, ts.state_name, tc.city_name, tut.user_type_name, tul.username' )
                                    ->join( 'tbl_user_types tut', 'tut.user_type_id = tbl_users.user_type_id' )
                                    ->join( 'tbl_user_login tul', 'tul.user_id = tbl_users.user_id' )
                                    ->join( 'tbl_states ts', 'ts.state_id = tbl_users.state_id', 'left' )
                                    ->join( 'tbl_cities tc', 'tc.city_id = tbl_users.city_id', 'left' )        
                                    ->whereIn( 'tbl_users.user_id', $arrintUserIds )
                                    ->where( 'tbl_users.is_deleted', NOT_DELETED )
                                    ->orderBy( 'tbl_users.user_id', 'desc' )
                                    ->get();

        return $objQuery->getResultArray();
    }
    
    public function findUserByUsername( $strUsername ) {
        
        $objQuery = $this->createBuilderObject()->select( 'tbl_users.*, tul.username, tul.password, ts.state_name, tc.city_name' )
                                    ->join( 'tbl_user_login tul', 'tul.user_id = tbl_users.user_id' )
                                    ->join( 'tbl_states ts', 'ts.state_id = tbl_users.state_id' )
                                    ->join( 'tbl_cities tc', 'tc.city_id = tbl_users.city_id' )   
                                    ->where( 'tul.username', $strUsername )
                                    ->where( 'is_deleted', NOT_DELETED )
                                    ->get();
                                    
        return $objQuery->getRowArray();
    }
    
}