<?php

namespace Modules\Category\Repositories;

use Illuminate\Database\Eloquent\Collection;
use Modules\Category\Models\Category;
use Modules\Category\Repositories\Interfaces\CategoryRepositoryInterface;

class CategoryRepository implements CategoryRepositoryInterface
{
    public function __construct(
        private Category $model
    ) {}

    public function all(): Collection
    {
        return $this->model->all();
    }

    public function find(int $id): ?Category
    {
        return $this->model->with('media')->find($id);
    }

    public function findBySlug(string $slug): ?Category
    {
        return $this->model->with('media')->where('slug', $slug)->first();
    }

    public function create(array $data): Category
    {
        return $this->model->create($data);
    }

    public function update(int $id, array $data): Category
    {
        $category = $this->find($id);
        $category->update($data);
        return $category->fresh();
    }

    public function delete(int $id): bool
    {
        return $this->model->destroy($id);
    }

    public function getActive(): Collection
    {
        return $this->model->with('media')->where('is_active', true)->orderBy('sort_order')->get();
    }
}
