/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Resource/ResourcesResource.php
<?php

namespace App\Http\Resources\Pages\Resource;

use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Career\VacancyCollection;
use App\Models\Page;
use App\Models\Vacancy;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;
/**
 * @OA\Schema(title="ResourcesResource", description="Resources resource",
 *  @OA\Xml(name="ResourcesResource")
 * )
 */
class ResourcesResource extends JsonResource
{
    /**
     * @OA\Property(property="fs_title", type="string", example=""),
     * @OA\Property(property="fs_title_background", type="string", example=""),
     * @OA\Property(property="fs_view_button", type="string", example=""),
     * @OA\Property(property="agricultural_tag", type="string", example=""),
     * @OA\Property(property="agricultural_title", type="string", example=""),
     * @OA\Property(property="agricultural_description", type="string", example=""),
     * @OA\Property(property="crop_nutrition_programs", type="object",
     *      @OA\Property(property="preview_title", type="string", example=""),
     *      @OA\Property(property="preview_text", type="string", example=""),
     *      @OA\Property(property="preview_link_text", type="string", example=""),
     *      @OA\Property(property="preview_image", type="string", example=""),
     * ),
     * @OA\Property(property="trial_results", type="object",
     *      @OA\Property(property="preview_title", type="string", example=""),
     *      @OA\Property(property="preview_text", type="string", example=""),
     *      @OA\Property(property="preview_link_text", type="string", example=""),
     *      @OA\Property(property="preview_image", type="string", example=""),
     * ),
     * @OA\Property(property="information_materials", type="object",
     *      @OA\Property(property="preview_title", type="string", example=""),
     *      @OA\Property(property="preview_text", type="string", example=""),
     *      @OA\Property(property="preview_link_text", type="string", example=""),
     *      @OA\Property(property="preview_image", type="string", example=""),
     * ),
     */
    public string|null $name;
    public string|null $language;
    public string|null $country;
    public array|null $attributes;

    public function __construct($resource, $language, $country)
    {
        parent::__construct($resource);
        $this->name = $resource->name;
        $this->language = $language;
        $this->country = $country;
        $this->attributes = $resource->data;
    }

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $crop_nutrition_programs = Page::query()->where('name', 'Crop nutrition programs')->first();
        $crop_nutrition_programs = $crop_nutrition_programs && $crop_nutrition_programs->data ? $crop_nutrition_programs->data : [];
        $trial_results = Page::query()->where('name', 'Trial results')->first();
        $trial_results = $trial_results && $trial_results->data ? $trial_results->data : [];
        $information_materials = Page::query()->where('name', 'Information materials')->first();
        $information_materials = $information_materials && $information_materials->data ? $information_materials->data : [];

        return [
            'fs_title' => ResourceHelper::getValue($this->attributes['fs_title'] ?? null, $this->country, $this->language),
            'fs_title_background' => ResourceHelper::getImage($this->attributes['fs_title_background'] ?? null),
            'fs_view_button' => ResourceHelper::getValue($this->attributes['fs_view_button'] ?? null, $this->country, $this->language),
            'agricultural_tag' => ResourceHelper::getValue($this->attributes['agricultural_tag'] ?? null, $this->country, $this->language),
            'agricultural_title' => ResourceHelper::getValue($this->attributes['agricultural_title'] ?? null, $this->country, $this->language),
            'agricultural_description' => ResourceHelper::getValue($this->attributes['agricultural_description'] ?? null, $this->country, $this->language),
            'crop_nutrition_programs' => [
                'preview_title' => ResourceHelper::getValue($crop_nutrition_programs['preview_title'] ?? null, $this->country, $this->language),
                'preview_text' => ResourceHelper::getValue($crop_nutrition_programs['preview_text'] ?? null, $this->country, $this->language),
                'preview_link_text' => ResourceHelper::getValue($crop_nutrition_programs['preview_link_text'] ?? null, $this->country, $this->language),
                'preview_new_image' => ResourceHelper::getImage($crop_nutrition_programs['preview_new_image'] ?? null),
            ],
            'trial_results' => [
                'preview_title' => ResourceHelper::getValue($trial_results['preview_title'] ?? null, $this->country, $this->language),
                'preview_text' => ResourceHelper::getValue($trial_results['preview_text'] ?? null, $this->country, $this->language),
                'preview_link_text' => ResourceHelper::getValue($trial_results['preview_link_text'] ?? null, $this->country, $this->language),
                'preview_image' => ResourceHelper::getImage($trial_results['preview_image'] ?? null),
            ],
            'information_materials' => [
                'preview_title' => ResourceHelper::getValue($information_materials['preview_title'] ?? null, $this->country, $this->language),
                'preview_text' => ResourceHelper::getValue($information_materials['preview_text'] ?? null, $this->country, $this->language),
                'preview_link_text' => ResourceHelper::getValue($information_materials['preview_link_text'] ?? null, $this->country, $this->language),
                'preview_image' => ResourceHelper::getImage($information_materials['preview_image'] ?? null),
            ]
        ];
    }

}