<?php

namespace App\Transformers\Registries;

use App\Models\Registries\Category;
use App\Services\PermissionsService;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class CategoryTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [

    ];

    protected array $availableIncludes = [
        'registry', 'parent', 'children', 'entries', 'permissions'
    ];

    public function transform(Category $model): array {
        return [
            'id' => $model->uuid,
            'name' => $model->name,
            'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null,
            'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null
        ];
    }

    public function includeRegistry(Category $model): ?Item {
        return $model->registry ? $this->item($model->registry, new RegistryTransformer()) : null;
    }

    public function includeParent(Category $model): ?Item {
        return $model->parent ? $this->item($model->parent, new CategoryTransformer()) : null;
    }

    public function includeChildren(Category $model): Collection {
        return $this->collection($model->children, new CategoryTransformer());
    }

    public function includeEntries(Category $model): Collection {
        return $this->collection($model->entries, new EntryTransformer());
    }

    public function includePermissions(Category $model): Primitive {
        return $this->primitive((new PermissionsService($model))->get());
    }


}
