<?php

namespace App\Transformers\Applications;

use App\Models\Applications\Application;
use App\Services\PermissionsService;
use App\Transformers\Objects\ObjectTransformer;
use App\Transformers\Products\ProductTransformer;
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 ApplicationTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [];

    protected array $availableIncludes = [
        'submitter', 'product', 'properties', 'conclusions', 'permissions'
    ];

    public function transform(Application $model): array {
        return [
            'id' => $model->uuid,
            'title' => $model->title,
            'status' => $model->parsedStatus,
            'number' => $model->number,
            'applicant' => $model->applicant,
            'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null,
            'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null
        ];
    }

    public function includeSubmitter(Application $model): ?Item {
        return $model->submitter ? $this->item($model->submitter, new UserTransformer()) : null;
    }

    public function includeProduct(Application $model): ?Item {
        return $model->product ? $this->item($model->product, new ProductTransformer()) : null;
    }

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

    public function includeConclusions(Application $model): Collection {
        return $this->collection($model->conclusions, new ConclusionTransformer());
    }

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

}