<?php

namespace App\Models;

use App\Entities\Chat\ChatNotificationEntity;
use CodeIgniter\Model;

class ChatNotification extends Model
{
    protected $table            = 'chat_notifications';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = ChatNotificationEntity::class;
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        "chat_message_id",
        "user_id",
        "viewed"
    ];

    protected bool $allowEmptyInserts = false;
    protected bool $updateOnlyChanged = true;

    protected array $casts = [];
    protected array $castHandlers = [];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_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 isChatMessageViewed(int $chat_message_id) : bool
    {
        $status = false;
        $count = $this->where("chat_message_id",$chat_message_id)->where("viewed",false)->countAllResults();
        if($count>0){
            $status = false;
        }else{
            $status = true;
        }
        return $status;
    }
    public function setNotificationsAsViewed(array $notificationIds) : bool
    {
        return $this->update($notificationIds,["viewed" => 1]);
    }
    public function setNotificationsAsUnViewed(array $notificationIds) : bool
    {
        return $this->update($notificationIds,["viewed" => 0]);
    }
}
