<?php

namespace App\Models;

use App\Core\Model;

class BlogCategory extends Model
{
    protected static string $table = 'iw_blog_categories';

    /**
     * All categories with number of published posts.
     */
    public static function allWithPostCount(): array
    {
        return static::db()->fetchAll(
            "SELECT c.*, COUNT(p.id) as post_count
             FROM iw_blog_categories c
             LEFT JOIN iw_blog_posts p ON p.category_id = c.id AND p.status = 'published'
             GROUP BY c.id
             ORDER BY c.name ASC"
        );
    }
}
