<?php

namespace App\Http\Resources;

use App\Http\Controllers\Helpers\HelperController;
use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

class CommentsResource 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,
            'comment' => $this->comment,
            'role' => HelperController::get_user_role($this->role),
            'name' => $this->get_user_name($this->user_id),
            'user_id' => $this->user_id,
            'signature' => $this->get_user_signature($this->user_id),
            'date' => $this->pretty_date($this->created_at)
        ];
    }

    private function get_user_name($id)
    {
        $user = User::where('id', $id)->first();
        return $user->name;
    }

    private function get_user_signature($id)
    {
        $user = User::where('id', $id)->first();
        return $user->signature;
    }

    private function pretty_date($date)
    {
        return date('M d, Y', strtotime($date));
    }
}