<?php

namespace App\Transformers\Registries;

use App\Models\Registries\Registry;
use App\Services\PermissionsService;
use App\Transformers\Objects\ObjectTransformer;
use App\Transformers\Pages\PageTransformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class RegistryTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [

    ];

    protected array $availableIncludes = [
        'page', 'categories', 'entries', 'permissions'
    ];

    public function transform(Registry $model): array {
        return [
            'id' => $model->uuid,
            'type' => $model->parsedType,
            '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 includePage(Registry $model): ?Item {
        return $model->page ? $this->item($model->page, new PageTransformer()) : null;
    }

    public function includeCategories(Registry $model): Collection {
        return $this->collection($model->rootCategories, new CategoryTransformer());
    }

    public function includeEntries(Registry $model): Collection {
        return $this->collection($model->objects, new ObjectTransformer());
    }

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


}
