<?php

namespace App\Transformers\Publications;

use App\Models\Asset;
use App\Models\Pages\Page;
use App\Models\Publications\Publication;
use App\Models\Publications\PublicationType;
use App\Services\PermissionsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Objects\ObjectTransformer;
use App\Transformers\Pages\PageTransformer;
use App\Transformers\Users\UserTransformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class PublicationTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [
        'poster'
    ];

    protected array $availableIncludes = [
        'page', 'parents', 'poster', 'author', 'sections', 'sidebars', 'permissions'
    ];

    public function transform(Publication $model): array {
        $params = $model->parsedParams;

        $result = [
            'id' => $model->uuid,
            'slug' => $model->slug,
            'link' => $model->link,
            'type' => 'publication',
            'subtype' => $model->parsedType,
            'params' => $params,
            'name' => $model->name,
            'excerpt' => $model->excerpt,
            'is_published' => boolval($model->is_published),
            'published_at' => $model->published_at ? $model->published_at->toIso8601String() : null,
            'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null,
            'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null
        ];

        if ($model->parsedType['name'] === PublicationType::PHOTOS && $params->assets) {
            $models = Asset::query()->whereIn('uuid', $params->assets)->orderBy('id')->get();
            $result['assets'] = fractal($models, new AssetTransformer());
        }

        return $result;
    }

    public function includePage(Publication $model): ?Item {
        return $model->page ? $this->item($model->page, new PageTransformer()) : null;
    }

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

    public function includePoster(Publication $model): ?Item {
        return $model->poster ? $this->item($model->poster, new AssetTransformer()) : null;
    }

    public function includeAuthor(Publication $model): ?Item {
        return $model->author ? $this->item($model->author, new UserTransformer()) : null;
    }

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

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

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


}
