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

namespace App\Http\Resources\Pages;

use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Banners\BannerCollection;
use App\Http\Resources\Pages\Blog\PostCollection;
use App\Http\Resources\Pages\Cards\MissionCardCollection;
use App\Http\Resources\Pages\Career\VacancyCollection;
use App\Http\Resources\Pages\Options\OptionCollection;
use App\Http\Resources\Pages\Products\ProductCollection;
use App\Models\Post;
use App\Models\Product;
use App\Models\Vacancy;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;
/**
 * @OA\Schema(title="HomepageResource", description="Homepage resource",
 *     @OA\Xml(name="HomepageResource")
 * )
 */
class HomepageResource extends JsonResource
{
    /**
     * @OA\Property(property="first_screen_title", type="string", example=""),
     * @OA\Property(property="fs_title_background", type="string", example=""),
     * @OA\Property(property="description", type="string", example=""),
     * @OA\Property(property="banner_title", type="string", example=""),
     * @OA\Property(property="banner_card_1", type="string", example=""),
     * @OA\Property(property="banner_card_2", type="string", example=""),
     * @OA\Property(property="banner_card_3", type="string", example=""),
     * @OA\Property(property="banner_card_4", type="string", example=""),
     * @OA\Property(property="about_tag", type="string", example=""),
     * @OA\Property(property="about_title", type="string", example=""),
     * @OA\Property(property="about_text", type="string", example=""),
     * @OA\Property(property="about_button", type="string", example=""),
     * @OA\Property(property="mission_title", type="string", example=""),
     * @OA\Property(property="mission_background_image", type="string", example=""),
     * @OA\Property(property="mission_cards", type="array",
     *      @OA\Items(type="object",ref="#/components/schemas/MissionCardResource"),
     * )
     * @OA\Property(property="products_tag", type="string", example=""),
     * @OA\Property(property="products_title", type="string", example=""),
     * @OA\Property(property="discover_trial_image", type="string", example=""),
     * @OA\Property(property="discover_trial_title", type="string", example=""),
     * @OA\Property(property="discover_trial_description", type="string", example=""),
     * @OA\Property(property="discover_nutrition_background", type="string", example=""),
     * @OA\Property(property="discover_nutrition_title", type="string", example=""),
     * @OA\Property(property="discover_nutrition_description", type="string", example=""),
     * @OA\Property(property="discover_information_title", type="string", example=""),
     * @OA\Property(property="discover_information_description", type="string", example=""),
     * @OA\Property(property="news_tag", type="string", example=""),
     * @OA\Property(property="news_title", type="string", example=""),
     * @OA\Property(property="news_description", type="string", example=""),
     * @OA\Property(property="news_button", type="string", example=""),
     * @OA\Property(property="instagram_title", type="string", example=""),
     * @OA\Property(property="instagram_photo_1", type="string", example=""),
     * @OA\Property(property="instagram_photo_2", type="string", example=""),
     * @OA\Property(property="instagram_photo_3", type="string", example=""),
     * @OA\Property(property="instagram_photo_4", type="string", example=""),
     * @OA\Property(property="cta_tag", type="string", example=""),
     * @OA\Property(property="cta_title", type="string", example=""),
     * @OA\Property(property="cta_image", type="string", example=""),
     * @OA\Property(property="cta_field_name_title", type="string", example=""),
     * @OA\Property(property="cta_field_name_placeholder", type="string", example=""),
     * @OA\Property(property="cta_field_email_title", type="string", example=""),
     * @OA\Property(property="cta_field_email_placeholder", type="string", example=""),
     * @OA\Property(property="cta_field_phone_title", type="string", example=""),
     * @OA\Property(property="cta_field_phone_placeholder", type="string", example=""),
     * @OA\Property(property="cta_field_organization_title", type="string", example=""),
     * @OA\Property(property="cta_field_organization_placeholder", type="string", example=""),
     * @OA\Property(property="cta_field_sender_title", type="string", example=""),
     * @OA\Property(property="cta_field_message_title", type="string", example=""),
     * @OA\Property(property="cta_field_message_placeholder", type="string", example=""),
     */
    public string|null $name;
    public string|null $language;
    public string|null $country;
    public array|null $data;

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

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
//        $products = Product::query()->where('is_enabled', true)->limit(6)->get();
//        $posts = Post::query()->where('is_enabled', true)->limit(6)->get();
//        dd($this->data['mission_cards']);
        return [
            'first_screen_title' => ResourceHelper::getValue($this->data['first_screen_title'] ?? null, $this->country, $this->language),
            'fs_title_background' => ResourceHelper::getImage($this->data['fs_title_title_background'] ?? null),
            'description' => ResourceHelper::getValue($this->data['fs_description'] ?? null, $this->country, $this->language),
            'banner_title' => ResourceHelper::getValue($this->data['banner_title'] ?? null, $this->country, $this->language),
            'banner_cards' => isset($this->data['banner_cards']) ? BannerCollection::make( is_array($this->data['banner_cards']) ? $this->data['banner_cards'] : json_decode($this->data['banner_cards']))->country($this->country)->language($this->language) : null,

//            'banner_card_1' => ResourceHelper::getValue($this->data['banner_card_1'] ?? null, $this->country, $this->language),
//            'banner_card_2' => ResourceHelper::getValue($this->data['banner_card_2'] ?? null, $this->country, $this->language),
//            'banner_card_3' => ResourceHelper::getValue($this->data['banner_card_3'] ?? null, $this->country, $this->language),
//            'banner_card_4' => ResourceHelper::getValue($this->data['banner_card_4'] ?? null, $this->country, $this->language),
            'about_tag' => ResourceHelper::getValue($this->data['about_tag'] ?? null, $this->country, $this->language),
            'about_title' => ResourceHelper::getValue($this->data['about_title'] ?? null, $this->country, $this->language),
            'about_text' => ResourceHelper::getValue($this->data['about_text'] ?? null, $this->country, $this->language),
            'about_button' => ResourceHelper::getValue($this->data['about_button'] ?? null, $this->country, $this->language),
            'mission_title' => ResourceHelper::getValue($this->data['mission_title'] ?? null, $this->country, $this->language),
            'mission_background_image' => ResourceHelper::getImage($this->data['mission_background_image'] ?? null),
            'mission_cards' => isset($this->data['mission_cards']) ? MissionCardCollection::make( is_array($this->data['mission_cards']) ? $this->data['mission_cards'] : json_decode($this->data['mission_cards']))->country($this->country)->language($this->language) : null,
//            'mission_card_1' => ResourceHelper::getValue($this->data['mission_card_1'] ?? null, $this->country, $this->language),
//            'mission_card_2' => ResourceHelper::getValue($this->data['mission_card_2'] ?? null, $this->country, $this->language),
//            'mission_card_3' => ResourceHelper::getValue($this->data['mission_card_3'] ?? null, $this->country, $this->language),
            'products_tag' => ResourceHelper::getValue($this->data['products_tag'] ?? null, $this->country, $this->language),
            'products_title' => ResourceHelper::getValue($this->data['products_title'] ?? null, $this->country, $this->language),
//            'products' => $products ? ProductCollection::make($products->collect())->country($this->country)->language($this->language)->full(false) : null,
            'discover_title' => ResourceHelper::getValue($this->data['discover_title'] ?? null, $this->country, $this->language),
            'discover_trial_image' => ResourceHelper::getImage($this->data['discover_trial_image'] ?? null),
            'discover_trial_title' => ResourceHelper::getValue($this->data['discover_trial_title'] ?? null, $this->country, $this->language),
            'discover_trial_description' => ResourceHelper::getValue($this->data['discover_trial_description'] ?? null, $this->country, $this->language),
            'discover_nutrition_background' => ResourceHelper::getImage($this->data['discover_nutrition_background'] ?? null),
            'discover_nutrition_title' => ResourceHelper::getValue($this->data['discover_nutrition_title'] ?? null, $this->country, $this->language),
            'discover_nutrition_description' => ResourceHelper::getValue($this->data['discover_nutrition_description'] ?? null, $this->country, $this->language),
            'discover_information_title' => ResourceHelper::getValue($this->data['discover_information_title'] ?? null, $this->country, $this->language),
            'discover_information_description' => ResourceHelper::getValue($this->data['discover_information_description'] ?? null, $this->country, $this->language),
            'news_tag' => ResourceHelper::getValue($this->data['news_tag'] ?? null, $this->country, $this->language),
            'news_title' => ResourceHelper::getValue($this->data['news_title'] ?? null, $this->country, $this->language),
            'news_description' => ResourceHelper::getValue($this->data['news_description'] ?? null, $this->country, $this->language),
            'news_button' => ResourceHelper::getValue($this->data['news_button'] ?? null, $this->country, $this->language),
//            'news_posts' => $posts ? PostCollection::make($posts->collect())->country($this->country)->language($this->language)->full(false) : null,
            'instagram_title' => ResourceHelper::getValue($this->data['instagram_title'] ?? null, $this->country, $this->language),
            'instagram_photo_1' => ResourceHelper::getImage($this->data['instagram_photo_1'] ?? null),
            'instagram_photo_2' => ResourceHelper::getImage($this->data['instagram_photo_2'] ?? null),
            'instagram_photo_3' => ResourceHelper::getImage($this->data['instagram_photo_3'] ?? null),
            'instagram_photo_4' => ResourceHelper::getImage($this->data['instagram_photo_4'] ?? null),

        ];
    }

}