<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class CommentResource extends JsonResource
{
  public function toArray(Request $request): array
  {
    return [
      'id' => $this->id,
      'user_id' => $this->user_id,
      'parent_id' => $this->parent_id,
      'commentable_type' => class_basename($this->commentable_type),
      'commentable_id' => $this->commentable_id,
      'body' => $this->body,
      'category' => $this->category,
      'status' => $this->status,
      'created_at' => $this->created_at?->toISOString(),
      'updated_at' => $this->updated_at?->toISOString(),
      'user' => new UserResource($this->whenLoaded('user')),
      'replies' => CommentResource::collection($this->whenLoaded('replies')),
    ];
  }
}
