<?php

namespace App\Transformers\Companies;

use App\Models\Companies\CompanyMember;
use App\Transformers\Advisories\AdvisoryMemberTransformer;
use App\Transformers\Users\UserTransformer;
use Illuminate\Support\Facades\Auth;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

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

    protected array $availableIncludes = [
        'user', 'company', 'department', 'advisoryMembership', 'isSubordinate'
    ];

    public function transform(CompanyMember $model): array {
        return [
            'id' => $model->uuid,
            'position' => $model->position,
            'rank' => $model->parsedRank,
            'role' => $model->parsedRole,
            'caption' => $model->user->name,
            'subtitle' => "{$model->company->name}, {$model->position}",
            'room' => $model->room,
            'intercom' => $model->intercom
        ];
    }

    public function includeUser(CompanyMember $model): ?Item {
        return $model->user ? $this->item($model->user, new UserTransformer()) : null;
    }

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

    public function includeDepartment(CompanyMember $model): ?Item {
        return $model->department ? $this->item($model->department, new DepartmentTransformer()) : null;
    }

    public function includeAdvisoryMembership(CompanyMember $model): Collection {
        return $this->collection($model->advisoryMembership, new AdvisoryMemberTransformer());
    }

    public function includeIsSubordinate(CompanyMember $model): ?Primitive {
        return $this->primitive($model->isSubordinateFor(Auth::user()));
    }


}
