<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Models;

use Illuminate\Support\Facades\Hash;
use Validator;

/**
 * Description of UserModel
 *
 * @author jehm2
 */
class UserModel
{

    public static function getUsers($filters = array(), $paginate = 10)
    {
        $query = User::query();
        return $paginate ? $query->paginate($paginate) : $query->get();
    }

    public static function getUser($user_id)
    {
        return User::find($user_id);
    }

    public static function getUsersQuery($request)
    {
        $query = User::select('users.*', 'profiles.name as profileName', 'companies.name as companyName');
        $query->leftJoin('profiles', 'profiles.profile_id', 'users.profile_id');
        $query->leftJoin('companies', 'companies.company_id', 'users.company_id');

        if (isset($request->keyword)) {
            $query->where(function ($sQuery) use ($request) {

                $sQuery->where('first_name', 'like', "%{$request->keyword}%");
                $sQuery->where('last_name', 'like', "%{$request->keyword}%");
                $sQuery->where('email', 'like', "%{$request->keyword}%");
                $sQuery->where('profileName', 'like', "%{$request->keyword}%");
                $sQuery->where('companyName', 'like', "%{$request->keyword}%");
            });
        }

        return $query;
    }

    public static function save($data)
    {
        $user = UserModel::getUser($data['user_id'] ?? null);
        if ($user) {
            if ($data['password'] != $user->password) {
                $data['password'] = Hash::make($data['password']);
            }
            $user->update($data);
        } else {
            $data['password'] = Hash::make($data['password']);
            $user = User::create($data);
        }
        return $user;
    }

    public static function delete($user_id)
    {
        $user = UserModel::getUser($user_id);
        if ($user) {
            return $user->delete();
        }
        return false;
    }

    public static function getValidator($data)
    {

        $user_id = $data['user_id'] ?? null;

        $rules = [
            'profile_id' => ['required', 'exists:profiles,profile_id'],
            'first_name' => ['required', 'max:255'],
            'last_name' => ['max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,' . $user_id . ',user_id'],
            'password' => ['required', 'string', 'min:8'],
        ];

        $messages = ModelUtils::getMessages();

        $validator = Validator::make($data, $rules, $messages);

        $niceNames = array(
            'profile_id' => 'Perfil',
            'first_name' => 'Nombres',
            'last_name' => 'Apellidos',
            'email' => 'Email',
            'password' => 'Password',

        );
        $validator->setAttributeNames($niceNames);

        return $validator;
    }
}
