<?php

namespace App\Models;

use CodeIgniter\Model;

class NewsCommentModel extends Model
{
    protected $table         = 'news_comments';
    protected $primaryKey    = 'id';
    protected $returnType = 'array';
    protected $allowedFields = ['news_id', 'parent_id', 'name', 'email', 'avatar', 'comment', 'is_approved'];
    protected $useTimestamps = false;
    protected $createdField  = 'created_at';
    protected $updatedField  = null;

    public function getByNews(int $newsId)
    {
        return $this->where('news_id', $newsId)
                    ->where('is_approved', 1)
                    ->orderBy('created_at', 'ASC')
                    ->findAll();
    }

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