<?php

namespace App\Transformers\Registries;

use App\Models\Registries\Entry;
use App\Services\PermissionsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Objects\ObjectTransformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class EntryTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [

    ];

    protected array $availableIncludes = [
        'registry', 'category', 'asset', 'operations', 'properties'
    ];

    public function transform(Entry $model): array {
        return [
            'id' => $model->uuid,
            'number' => $model->number,
            'name' => $model->name,
            'state' => $model->state,
            'active_since' => $model->active_since ? $model->active_since->toIso8601String() : null,
            'active_till' => $model->active_till ? $model->active_till->toIso8601String() : null,
            'suspended_since' => $model->suspended_since ? $model->suspended_since->toIso8601String() : null,
            'suspended_till' => $model->suspended_till ? $model->suspended_till->toIso8601String() : null,
            'cancelled_at' => $model->cancelled_at ? $model->cancelled_at->toIso8601String() : null,
            'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null,
            'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null
        ];
    }

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

    public function includeCategory(Entry $model): ?Item {
        return $model->category ? $this->item($model->category, new CategoryTransformer()) : null;
    }

    public function includeAsset(Entry $model): ?Item {
        return $model->asset ? $this->item($model->asset, new AssetTransformer()) : null;
    }

    public function includeOperations(Entry $model): Collection {
        return $this->collection($model->operations, new ObjectTransformer());
    }

    public function includeProperties(Entry $model): ?Item {
        return $model->properties ? $this->item($model->properties, new ObjectTransformer()) : null;
    }

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


}
