<?php

namespace App\Http\Resources\Company\Project;

use Illuminate\Support\Facades\Log;
use App\Http\Resources\WorkerBillableRate;
use Illuminate\Http\Resources\Json\JsonResource;

class Supervisor extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'project_id' => $this->project_id,
            'user_id' => $this->user_id,
            'date_from' => $this->date_from,
            'date_to' => $this->date_to,
            'profile_picture' => $this->profile_picture,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'profile_link' => $this->getProfileLink($this->profile_picture),
            'flat_rate' => $this->flat_rate,
            'rate' => $this->rate,
            'billable_rate' => new WorkerBillableRate($this->worker->workerBillableRate),
        ];
    }

    public function isBase64Profile($profilePicture){
        $wordToFind = 'data:image';
        return strpos($profilePicture, $wordToFind) !== false;
    }   

    public function getProfileLink($profilePicture){
        if(!$profilePicture){
            return null;
        }
        if($this->isBase64Profile($profilePicture)){
            return null;
        }
        return env('GOOGLE_CLOUD_LINK') .'/'. $this->profile_picture;
    }  
}
