<?php

namespace App\Models;

use CodeIgniter\Model;

class NotificationModel extends Model
{
    protected $table            = 'notifications';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'user_id',
        'task_id',
        'occurrence_id',
        'kind',
        'title',
        'message',
        'is_read',
        'read_at',
        'created_at',
    ];
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';

    public function findOwned(int $userId, int $notificationId): ?array
    {
        $row = $this->where('user_id', $userId)->find($notificationId);

        return $row === null ? null : $row;
    }

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