<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table = 'admin.users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['username', 'password_hash', 'email', 'active', 'created_at'];
    protected $useTimestamps = false;
    protected $returnType = 'array';

    public function getUserByUsername($username)
    {
        return $this->where('username', $username)->first();
    }

    public function getGroupId($userId)
    {
        $builder = $this->db->table('admin.user_group');
        $builder->select('group_id');
        $builder->where('user_id', $userId);
        $query = $builder->get();
        $result = $query->getRowArray();
        return $result['group_id'] ?? null;
    }

	public function buscarUsuarioMail($email) 
    {
    	$db = db_connect();
        $builder = $db->table($this->table)->where('email',$email)->where('l_estado', 'A');
    	$resultado = $builder->get();
        return $resultado->getResult() ? $resultado->getResult()[0] : false;
    }

	public function buscarUsuarioUsuario($usuario) 
    {
    	$db = db_connect();
        $builder = $db->table($this->table)->where('username',$usuario)->where('l_estado', 'A');
    	$resultado = $builder->get();
        return $resultado->getResult() ? $resultado->getResult()[0] : false;
    }
}
