<?php
declare(strict_types=1);
namespace Repositories;

final class CategoryRepository extends BaseRepository
{
    public function all(): array
    {
        return $this->fetchAll('SELECT c.*, COUNT(p.id) product_count FROM categories c LEFT JOIN products p ON p.category_id=c.id AND p.status="active" WHERE c.status="active" GROUP BY c.id ORDER BY c.sort_order ASC, c.name ASC');
    }

    public function findBySlug(string $slug): ?array
    {
        return $this->fetchOne('SELECT * FROM categories WHERE slug=:slug AND status="active" LIMIT 1', ['slug' => $slug]);
    }
}
