<?php

namespace App\Transformers\Advisories;

use App\Models\Advisories\Advisory;
use App\Services\PermissionsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Companies\CompanyMemberTransformer;
use App\Transformers\Companies\CompanyTransformer;
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 AdvisoryTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [];

    protected array $availableIncludes = [
        'logo', 'document', 'companies', 'advisoryCompanies', 'members', 'secretary', 'parent', 'children', 'object', 'permissions'
    ];

    public function transform(Advisory $model): array {
        return [
            'id' => $model->uuid,
            'type' => $model->parsedType,
            'name' => $model->name,
            'title' => $model->title,
            'number' => $model->number,
            'email' => $model->email,
            'phone' => $model->phone,
            'director_name' => $model->director_name,
            'secretary_name' => $model->secretary_name,
            'is_main' => boolval($model->is_main),
            'caption' => $model->caption
        ];
    }

    public function includeLogo(Advisory $model): ?Item {
        return $model->logo ? $this->item($model->logo, new AssetTransformer()) : null;
    }

    public function includeDocument(Advisory $model): ?Item {
        return $model->document ? $this->item($model->document, new AssetTransformer()) : null;
    }

    public function includeCompanies(Advisory $model): Collection {
        return $this->collection($model->companies, new CompanyTransformer());
    }

    public function includeAdvisoryCompanies(Advisory $model): Collection {
        return $this->collection($model->advisoryCompanies, new AdvisoryCompanyTransformer());
    }

    public function includeMembers(Advisory $model): Collection {
        return $this->collection($model->members, new AdvisoryMemberTransformer());
    }

    public function includeSecretary(Advisory $model): ?Item {
        return $model->secretary ? $this->item($model->secretary, new CompanyMemberTransformer()) : null;
    }

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

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

    public function includeObject(Advisory $model): ?Item {
        return $model->object ? $this->item($model->object, new ObjectTransformer()) : null;
    }

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


}