<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResourceList extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */

    public function toArray($request)
    {
        $member = $this->member;

        $out = [
            'id'                => $this->id,
            'name'              => $this->name,
            'lastName'          => $this->last_name,
            'email'             => $this->email,
            'department'        => $this->department,
            'function'          => $this->function,
            'phoneNumber'       => $this->phone_number,
            'imageLinks'        => getProfileImage($this->image, ['100x100']),
            'profileStatus'     => $this->profile_status,
            'state'             => $this->state,
        ];

        if (!empty($member)) {
            $out['member'] = new MemberResource($member);
        }

        if (!empty($request->input('committeeId')) && !empty($this->committees)) {
            $committee = $this->committees->where('status', 'published')->firstWhere('id', $request->input('committeeId'));
            if ($committee) {
                $out['typeList'] = $committee->pivot->type;
            }
        }

        if (!empty($request->input('workGroupId')) && !empty($this->workGroups)) {
            $workGroup = $this->workGroups->where('status', 'published')->firstWhere('id', $request->input('workGroupId'));
            if ($workGroup) {
                $out['typeList'] = $workGroup->pivot->type;
            }
        }

        return $out;
    }
}
