<?php

namespace App\Transformers\Advisories;

use App\Models\Advisories\AdvisoryCompany;
use App\Models\Advisories\AdvisoryMemberRank;
use App\Services\PermissionsService;
use App\Transformers\Companies\CompanyMemberTransformer;
use App\Transformers\Companies\CompanyTransformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class AdvisoryCompanyTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [];

    protected array $availableIncludes = [
        'advisory', 'company', 'voter', 'advisoryMembers', 'companyMembers', 'permissions'
    ];

    public function transform(AdvisoryCompany $model): array {
        return [
            'id' => $model->uuid,
            'title' => $model->title
        ];
    }

    public function includeAdvisory(AdvisoryCompany $model): ?Item {
        return $model->advisory ? $this->item($model->advisory, new AdvisoryTransformer()) : null;
    }

    public function includeCompany(AdvisoryCompany $model): ?Item {
        return $model->company ? $this->item($model->company, new CompanyTransformer()) : null;
    }

    public function includeVoter(AdvisoryCompany $model): ?Item {
        return $model->voter ? $this->item($model->voter, new AdvisoryMemberTransformer()) : null;
    }

    public function includeAdvisoryMembers(AdvisoryCompany $model): Collection {
        return $this->collection($model->advisoryMembers()->whereIn('rank', [AdvisoryMemberRank::ORDINARY, AdvisoryMemberRank::VOTER])->get(), new AdvisoryMemberTransformer());
    }

    public function includeCompanyMembers(AdvisoryCompany $model): Collection {
        return $this->collection($model->companyMembers, new CompanyMemberTransformer());
    }

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

}