<?php

namespace App\Http\Resources\Company;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Company\Project\Supervisor;
use App\Http\Resources\Company\Project\AssignedEmployee;
use Illuminate\Support\Facades\DB;
use App\Models\AssignedWorkers;

class ProjectDetails 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_manager_profile_picture' => $this->profile_picture,
            'project_manager_id' => $this->project_manager_id,
            'project_manager_name' => $this->first_name.' '.$this->last_name,
            'name' => $this->name,
            'description' => $this->description,
            'worker_items' => $this->worker_items,
            'additional_info' => $this->additional_info,
            'location' => $this->location,
            'clock_in' => date('Y-m-d H:i', strtotime($this->clock_in)),
            'clock_out' => date('Y-m-d H:i', strtotime($this->clock_out)),
            'date_start' => $this->date_start,
            'date_end' => $this->date_end,
            'project_supervisors' => Supervisor::collection($this->project_supervisors),
            'arrayOfEmployees' => AssignedEmployee::collection($this->assigned_employees),
            // 'arrayOfEmployees' => $this->assigned_employees,
            'arrayOfEmployeesCount' => count($this->assigned_employees),
            'noOfEmployeesAssigned' => $this->group_by('worker_id', $this->assigned_employees),
            'profile_link' => $this->getProfileLink($this->profile_picture),
            'totalSumOfWorkerRates' => $this->totalSumOfWorkerRatesAssignedOnProject($this->id),
            'numberOfWorker' => $this->numberOfWorker($this->assigned_employees),
        ];
    }

    public function group_by($key, $data) {
        $count = 0;
        foreach($data as $val) {
            if(!property_exists($key, $val)) {
                $count++;
            }
        }
        return $count;
    }

    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;
    }  

    public function totalSumOfWorkerRatesAssignedOnProject($projectID){
        $data = AssignedWorkers::select(
            DB::raw('SUM(workers.rate) as total_rate'),
        )
        ->join('workers', 'workers.id', 'assigned_workers.worker_id')
        ->where('project_id', $projectID)
        ->first();

        if(!$data->total_rate){
            return 0;
        }
        return $data->total_rate;
    }

    public function numberOfWorker($workers){
        $arrayedWorkers = (array) $workers;
        return count($arrayedWorkers);
    }
}
