<?php

namespace App\Models\Mobile;

use CodeIgniter\Model;
use Predis\Client;

class UserModel extends Model
{
    protected $DBGroup              = 'default';
    protected $table                = 'm_siswa';
    protected $primaryKey           = 'id';
    protected $useAutoIncrement     = true;
    protected $insertID             = 0;
    // protected $returnType           = 'array';
    protected $returnType           = \App\Entities\Mobile\User::class;
    protected $useSoftDeletes       = false;
    protected $protectFields        = true;
    protected $allowedFields        = [
        'nomor_induk', 'attendance_solution_id', 'rfid',
        'nama', 'agama', 'jenis_kelamin', 'tempat_lahir',
        'tanggal_lahir', 'alamat', 'email', 'phone',
        'password_hash', 'ruang_id', 'kelas_id', 'photo',
        'status', 'active_at'
    ];

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

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks       = true;
    protected $beforeInsert         = [];
    protected $afterInsert          = [];
    protected $beforeUpdate         = [];
    protected $afterUpdate          = [];
    protected $beforeFind           = [];
    protected $afterFind            = [];
    protected $beforeDelete         = [];
    protected $afterDelete          = [];
    public function findId($id)
    {
        $predis = null;
        $user = new \App\Entities\Mobile\User();
        if (redison()) {
            try {
                $redis  = json_decode(options('redis-config'), true);
                $predis = new Client([
                    'scheme' => 'tcp',
                    'host'   => $redis['host'],
                    'port'   => $redis['port'],
                    'password' => $redis['password'],
                    'database' => $redis['database'],
                ]);

                $predis->connect();
                $result = $predis->get("mobile-user-account-$id");
                if ($result) {
                    $user->fill(json_decode($result, true));
                    return $user;
                }
            } catch (\Throwable $th) {
                $predis = null;
            }
        }
        $bu = base_url(config('app')->imgPath . "/users");
        $tb = $this->table;
        $select = ["$tb.id", "$tb.nomor_induk", "$tb.nama", "$tb.email", "$tb.phone", "$tb.agama", "$tb.status"];
        $select[] = "$tb.jenis_kelamin,$tb.tempat_lahir,$tb.tanggal_lahir,$tb.alamat,$tb.created_at,$tb.updated_at,$tb.active_at";
        $select[] = "CONCAT('$bu/',$tb.photo) photo,mk.id id_kelas,mk.nama nama_kelas,mk.kode kode_kelas,mj.id id_jurusan";
        $select[] = "mj.kode kode_jurusan,mj.nama nama_jurusan,mru.id id_ruang,mru.kode kode_ruang";
        $select[] = "mru.nama nama_ruang";
        $builder = $this->db->table($tb);
        $builder->select(implode(',', $select));
        $builder->join("m_ruangujian mru", "mru.id=$tb.ruang_id", "left");
        $builder->join("m_kelas mk", "mk.id=$tb.kelas_id", "left");
        $builder->join("kelas_data kd", "kd.kelas_id=mk.id", "left");
        $builder->join("m_jurusan mj", "mj.id=kd.jurusan_id", "left");
        $builder->where("$tb.id", $id);
        $result = $builder->get()->getRowArray();
        if ($predis && $result) $predis->set("mobile-user-account-$id", json_encode($result), 'EX', 60 * 20);
        $user->fill($result);
        return $user;
    }
}
