<?php

namespace App\Http\Resources;

use App\Models\Plant;
use Illuminate\Http\Resources\Json\JsonResource;

class AssignedClerksResource 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,
            'name' => $this->name,
            'phone' => $this->phone,
            'email' => $this->email,
            'plant' => count($this->assigned_locations) > 0 ? $this->establish_plant($this->assigned_locations) : "Not assigned"
        ];
    }

    public function establish_plant($items)
    {
        $item = $items[0];
        $plant = Plant::where('plant_code', $item->plant_code)->first();
        return 'Assigned to ' . $plant->plant_name;
    }
}