<?php

namespace App\Models;

class NotificationModel extends TenantModel
{
    protected $table         = 'notifications';
    protected $primaryKey    = 'id';
    protected $returnType    = 'object';
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = '';

    protected $allowedFields = ['message', 'link', 'is_read'];

    public function notify(string $message, ?string $link = null): void
    {
        $this->insert(['message' => $message, 'link' => $link, 'is_read' => 0]);
    }

    public function unread(): array
    {
        return $this->where('is_read', 0)->orderBy('created_at', 'desc')->findAll();
    }

    public function unreadCount(): int
    {
        return $this->where('is_read', 0)->countAllResults();
    }

    public function markAllRead(): void
    {
        $this->where('is_read', 0)->update(null, ['is_read' => 1]);
    }

    public function markOneRead(int $id): void
    {
        $this->update($id, ['is_read' => 1]);
    }
}
