/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Blog/PostResource.php
<?php
namespace App\Http\Resources\Pages\Blog;

use App\Helpers\ResourceHelper;
use App\Models\Post;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;

/**
 * @OA\Schema(title="PostResource",description="Post resource",
 *     @OA\Xml(name="PostResource")
 * )
 */
class PostResource extends JsonResource
{

    /**
     * @OA\Property(property="slug", type="string", example="challenges_of_the_21st_century"),
     * @OA\Property(property="type", type="string", example="News"),
     * @OA\Property(property="title", type="string", example="Challenges of the 21st century"),
     * @OA\Property(property="title_background", type="string", example="https://uniferx-backend.test/posts/image.png"),
     * @OA\Property(property="data", type="string", example="<p>Microbial and viral pathogens, insect damage, weed infestation, and unknown nutrient deficiencies were the causes of poor harvests and even famine in Europe and around the world some 150-200 years ago.</p>"),
     */
    public string|null $language;
    public string|null $country;
    public string|null $time_to_read;
    public string|null $created_at;
    public bool $full;
    public string|null $slug;
    public string|null $title;
    public string|array|null $title_image;
    public array|null $data;
    public string|null $type;

    public function language($value): static
    {
        $this->language = $value;
        return $this;
    }

    public function country($value): static
    {
        $this->country = $value;
        return $this;
    }

    public function full($value = true): static
    {
        $this->full = $value;
        return $this;
    }

    public function __construct($resource, $language = null, $country = null, $full = true)
    {
        parent::__construct($resource);
//        dd($resource);
        $this->slug = $resource->slug;
        $this->title = $resource->title;
        $this->title_image = $resource->title_image;
        $this->time_to_read = $resource->time_to_read;
        $this->created_at = $resource->created_at;
        $this->data = $resource->data;
        $this->type = $resource->type;
        $this->country = $country;
        $this->language = $language;
        $this->full = $full;
    }



    public function toArray(Request $request): array
    {
        $data = [
            'slug' => $this->slug,
            'type' => $this->type,
            'title' => ResourceHelper::getValue($this->title, $this->country, $this->language),
            'time_to_read' => ResourceHelper::getValue($this->time_to_read, $this->country, $this->language),
            'title_background' => ResourceHelper::getImage($this->title_image),
            'crated_at' => Carbon::parse($this->created_at)->format('d.m.Y'),
        ];
        if ($this->full) {
//            if ($this->type === 'Article') {
                $data['fs_button'] = ResourceHelper::getValue($this->data['button_text'] ?? null, $this->country, $this->language);
                $data['up_text'] = ResourceHelper::getValue($this->data['up_text'] ?? null, $this->country, $this->language);
                $data['down_text'] = ResourceHelper::getValue($this->data['down_text'] ?? null, $this->country, $this->language);
                $data['section_text'] = SectionCollection::make($this->data['section_text'])->language($this->language)->country($this->country);
//            } else {
//                $data['text'] = ResourceHelper::getValue($this->data['up_text'] ?? null, $this->country, $this->language);
//            }
        }

        return $data;
    }

    public static function collection($resource): PostCollection
    {
        return new PostCollection($resource);
    }
}