<?php

namespace App\Http\Resources;

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

class PostCommentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $reportedCount = ReportedPostComment::where('post_comment_id', $this->id)->count();
        $isReported =  $reportedCount > 0 ? true : false;
        return [
            'id'        => $this->id,
            'post_id'   => $this->post_id,
            'user_id'   => $this->user_id,
            'user'      => $this->user,
            'comment'   => $this->comment,
            'is_reported' => $isReported,
            'active'    => $this->active,
            'reported_count' => $reportedCount,
            'created_at' =>  $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }

    public function withResponse($request, $response)
    {
        $charset = $request->header('Accept-Charset', 'utf-8');
        if ($charset === 'utf-8') {
            $response->header('Charset', 'utf-8');
            $response->header('Content-Type','application/json; charset=utf-8');
            $response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }
    }
}
