<?php

namespace App\Traits;

use Rinvex\Categories\Models\Category;
use Rinvex\Categories\Traits\Categorizable;

trait HasCategories
{
    use Categorizable;

    public function getHierarchicalCategoriesAttribute()
    {
        return [
            'category' => $this->mainCategory(),
            'sub_categories' => $this->specialties(),
        ];
    }

    public function mainCategory()
    {
        return Category::find($this->category_id, ['id', 'name']);

    }

    public function specialties()
    {
        return $this->categories()
            ->where('parent_id', $this->mainCategory()?->id)->get(['id', 'name']);
    }
}
