<?php

namespace App\Http\Resources;

use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Storage;

class HoseaResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray($request)
    {
        $result = collect(parent::toArray($request))->except(['created_at', 'updated_at']);

        $link = [];

        foreach ($this->get_image as $path) {
            array_push($link, $path->original_url);
        }
        $result['image'] = $link;

        $result['thumbnail'] = Storage::disk('public')->url($this->thumbnail);
        //$data1 = Article::where('id', $this->related_article)->get();
        //$result['related_article'] = ArticleResource::collection($data1);

        //$related = json_decode($this->related_article, true);
        // If json_decode returns null (invalid JSON or empty), set it to an empty array
        if ($this->related_article === null) {
            $this->related_article = [];
        }
        $related_article = Article::whereIn('id', $this->related_article)->get();
        $result['related_article'] = ArticleResource::collection($related_article);

        return $result;
    }
}