<?php

namespace App\Models\Panel\Elearning\JadwalMengajar;

use CodeIgniter\Model;

class Diskusi extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'jadwal_diskusi';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = false;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'id',
        'jadwal_id',
        'user_id',
        'user_type',
        'conversation',
    ];

    // 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    = [];
    static public function generateId()
    {
        return time() . uniqid('-');
    }
    public function getInitConvesations($jadwal_id, $limit = 10)
    {
        $jumlah = $this->db->table($this->table)->where('jadwal_id', $jadwal_id)->countAllResults();
        $select = ["jd.id,IFNULL(u.fullname,ms.nama) nama,(CASE WHEN u.id IS NULL THEN 0 ELSE 1 END) is_me"];
        $select[] = "jd.conversation,IFNULL(u.nomor_induk,ms.nomor_induk) nomor_induk,jd.created_at,jd.updated_at";
        $builder = $this->db->table("$this->table jd");
        $builder->select(implode(',', $select));
        $builder->join('m_siswa ms', "ms.id=jd.user_id AND jd.user_type='mobile'", 'left', false);
        $builder->join('users u', "u.id=jd.user_id AND jd.user_type='staff'", 'left', false);
        $builder->where('jd.jadwal_id', $jadwal_id);
        $builder->orderBy("jd.created_at", 'ASC', false);
        return $builder->get($limit, $jumlah > $limit ? $jumlah - $limit : 0)->getResultArray();
    }

    public function getOldest($jadwal_id, $lastid, $limit = 10)
    {
        $select = ["jd.id,IFNULL(u.fullname,ms.nama) nama,(CASE WHEN u.id IS NULL THEN 0 ELSE 1 END) is_me"];
        $select[] = "jd.conversation,IFNULL(u.nomor_induk,ms.nomor_induk) nomor_induk,jd.created_at,jd.updated_at";
        $builder = $this->db->table("$this->table jd");
        $builder->select(implode(',', $select));
        $builder->join('m_siswa ms', "ms.id=jd.user_id AND jd.user_type='mobile'", 'left', false);
        $builder->join('users u', "u.id=jd.user_id AND jd.user_type='staff'", 'left', false);
        $builder->where('jd.jadwal_id', $jadwal_id);
        $builder->where("SUBSTRING_INDEX(jd.id,' - ',1) <=", "SUBSTRING_INDEX('$lastid','-',1)", false);
        $builder->where('jd.id !=', $lastid);
        $builder->orderBy("SUBSTRING_INDEX(jd.id,'-',1)", 'DESC', false);
        return $builder->get($limit, 0)->getResultArray();
    }
    public function getNewest($jadwal_id, $lastid, $limit = 5)
    {
        $select = ["jd.id,IFNULL(u.fullname,ms.nama) nama,(CASE WHEN u.id IS NULL THEN 0 ELSE 1 END) is_me"];
        $select[] = "jd.conversation,IFNULL(u.nomor_induk,ms.nomor_induk) nomor_induk,jd.created_at,jd.updated_at";
        $builder = $this->db->table("$this->table jd");
        $builder->select(implode(',', $select));
        $builder->join('m_siswa ms', "ms.id=jd.user_id AND jd.user_type='mobile'", 'left', false);
        $builder->join('users u', "u.id=jd.user_id AND jd.user_type='staff'", 'left', false);
        $builder->where('jd.jadwal_id', $jadwal_id);
        $builder->where("SUBSTRING_INDEX(jd.id,' - ',1) >=", "SUBSTRING_INDEX('$lastid','-',1)", false);
        $builder->where('jd.id !=', $lastid);
        $builder->orderBy("SUBSTRING_INDEX(jd.id,'-',1)", 'ASC', false);
        return $builder->get($limit, 0)->getResultArray();
    }
}
