<?php

namespace App\Modules\Auth\Models;

use App\Modules\Auth\Entities\User;
use CodeIgniter\Model;

/**
 * User Model
 */
class UserModel extends Model
{
    protected $table            = 'users';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = User::class;
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'name',
        'email',
        'password_hash',
        'is_active',
    ];

    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    protected $validationRules = [
        'name'          => 'required|max_length[255]',
        'email'         => 'required|valid_email|max_length[255]|is_unique[users.email,id,{id}]',
        'password_hash' => 'required|max_length[255]',
        'is_active'     => 'permit_empty|in_list[0,1]',
    ];

    protected $validationMessages = [];
    protected $skipValidation     = false;
    protected $cleanValidationRules = true;

    public function findByEmail(string $email): ?User
    {
        return $this->where('email', $email)->first();
    }
}
