<?php
class Chat_model extends CI_Model
{

    public function get_all_chats()
    {
        $this->db->select('chat.*, tb_wali_santri.nama_wali_santri, tb_santri.nama_santri');
        $this->db->from('chat');
        $this->db->join('tb_wali_santri', 'tb_wali_santri.id_wali_santri = chat.id_wali_santri');
        $this->db->join('tb_santri', 'tb_santri.id_santri = tb_wali_santri.id_santri');
        $this->db->order_by('chat.timestamp', 'ASC');
        return $this->db->get()->result();
    }

    public function insertChat($id_wali_santri, $sender, $message)
    {
        $data = [
            'id_wali_santri' => $id_wali_santri,
            'sender' => $sender,
            'message' => $message,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        return $this->db->insert('chat', $data);
    }

    public function getChatByWali($id_wali_santri)
    {
        $this->db->where('id_wali_santri', $id_wali_santri);
        $this->db->order_by('timestamp', 'ASC');
        $query = $this->db->get('chat');
        return $query->result_array();
    }
}
