<?php

namespace App\Http\Resources\Company\Project;

use Illuminate\Support\Facades\Log;
use App\Http\Resources\WorkerBillableRate;
use App\Modules\Company\Employees\BillableRate;
use Illuminate\Http\Resources\Json\JsonResource;

class AssignedEmployee 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 [
            'project_id' => $this->project_id,
            'id' => $this->id,
            'worker_id' => $this->worker_id,
            'assigned_worker_id' => $this->assigned_worker_id,
            'start_date' => $this->start_date,
            'end_date' => $this->end_date,
            'profile_picture' => $this->profile_picture,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'worker_type' => $this->worker_type,
            'rate' => $this->rate,
            'flat_rate' => $this->flat_rate,
            'is_draft' => $this->is_draft,
            'experience_id' => $this->experience_id,
            'experience_name' => $this->experience_name,
            'profile_link' => $this->getProfileLink($this->profile_picture),
            '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;
    }  
}
