<?php

namespace App\Transformers\Pages;

use App\Models\Pages\Page;
use App\Services\PermissionsService;
use App\Transformers\Objects\ObjectTransformer;
use App\Transformers\Publications\PublicationTransformer;
use App\Transformers\Registries\RegistryTransformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class PageTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [

    ];

    protected array $availableIncludes = [
        'children', 'parent', 'parents', 'sections', 'sidebars', 'publications',
        'registries', 'registry', 'permissions'
    ];

    public function transform(Page $model): array {
        return [
            'id' => $model->uuid,
            'slug' => $model->slug,
            'link' => $model->link,
            'type' => $model->parsedType,
            'sub_type' => $model->sub_type,
            'name' => $model->name,
            'title' => $model->title,
            'h1' => $model->h1,
            'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null,
            'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null
        ];
    }

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

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

    public function includeParents(Page $model): Collection {
        return $this->collection($model->parents->reverse(), new PageTransformer());
    }

    public function includeSections(Page $model): Collection {
        return $this->collection($model->sections, new ObjectTransformer());
    }

    public function includeSidebars(Page $model): Collection {
        return $this->collection($model->sidebars, new ObjectTransformer());
    }

    public function includePublications(Page $model): Collection {
        return $this->collection($model->publications, new PublicationTransformer());
    }

    public function includeRegistries(Page $model): Collection {
        return $this->collection($model->registries, new RegistryTransformer());
    }

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

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


}
