<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ForumCategory extends Model
{
    protected $fillable = ['name', 'slug', 'description', 'order', 'moderation_enabled'];

    protected $casts = [
        'moderation_enabled' => 'boolean',
    ];

    public function threads(): HasMany
    {
        return $this->hasMany(ForumThread::class, 'forum_category_id')->orderByDesc('is_pinned')->orderByDesc('last_reply_at');
    }

    public function threadsCount(): int
    {
        return $this->threads()->count();
    }

    public function repliesCount(): int
    {
        return ForumReply::whereIn('forum_thread_id', $this->threads()->pluck('id'))->count();
    }
}
