<?php

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,
            'content'     => $this->content,
            'created_at'  => $this->created_at->toISOString(),
            'user'        => new UserResource($this->whenLoaded('user')),
            'like_count'  => $this->likes_count ?? $this->likes()->count(),
            'liked_by_me' => $this->whenLoaded('likes', fn() =>
                $this->likes->contains('user_id', $request->user()?->id)
            ),
            'replies' => ReplyResource::collection($this->whenLoaded('replies')),
        ];
    }
}
