<?php

namespace App\Repositories\Eloquent;

use App\Enums\FaqCategory;
use App\Models\Faq;
use App\Repositories\Contracts\FaqRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;

/**
 * @extends BaseRepository<Faq>
 */
class FaqRepository extends BaseRepository implements FaqRepositoryInterface
{
    public function __construct(Faq $model)
    {
        parent::__construct($model);
    }

    /**
     * @param  array<string, mixed>  $filters
     * @return LengthAwarePaginator<int, Faq>
     */
    public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
    {
        $query = $this->model->newQuery();

        if (! empty($filters['category'])) {
            $query->where('category', $filters['category']);
        }

        return $query->orderBy('order_index')->paginate($perPage);
    }

    /**
     * @return Collection<int, Faq>
     */
    public function getByCategory(FaqCategory $category): Collection
    {
        return Cache::remember("faqs.{$category->value}", 3600, fn () => $this->model->newQuery()
            ->where('category', $category)
            ->where('is_active', true)
            ->orderBy('order_index')
            ->get());
    }
}
