<?php

namespace App\Transformers\Pages;

use App\Models\Pages\Page;
use App\Services\PermissionsService;
use App\Transformers\Assets\AssetTransformer;
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 {
    public ?bool $withTrashed = false;

    public function __construct(?bool $withTrashed = false) {
        $this->withTrashed = $withTrashed;
    }

    protected array $defaultIncludes = [

    ];

    protected array $availableIncludes = [
        'children', 'parent', 'parents', 'picture',  '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,
            'name' => $model->name,
            'title' => $model->title,
            'h1' => $model->h1,
            'description' => $model->description,
            'keywords' => $model->keywords,
            'has_children' => $model->children()->exists(),
            'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null,
            'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null,
            'deleted_at' => $model->deleted_at ? $model->deleted_at->toIso8601String() : null
        ];
    }

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

    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 includePicture(Page $model): ?Item {
        return $model->picture ? $this->item($model->picture, new AssetTransformer()) : null;
    }

    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());
    }


}
