<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table            = 'users';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = ['role_id', 'username', 'email', 'password_hash', 'is_active'];

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

    // Fetch user with role name
    public function getUserWithRole(string $username)
    {
        return $this->select('users.*, roles.nama_role')
                    ->join('roles', 'roles.id = users.role_id')
                    ->where('username', $username)
                    ->first();
    }
}
