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

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

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

    /**
     * @OA\Property(property="title", type="string", example="Kalyna farming enterprise  Ternopil region, 2023"),
     * @OA\Property(property="file", type="string", example="https://uniferx-backend.test/storage/crops/trials/X2YZbZ7anmN5W3QdUn3iJEgacNxdXNKP1KTQakt8.txt"),
     */
    public string|null $language;
    public string|null $country;
    public array|null $data;

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

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

    public function __construct($resource)
    {
        parent::__construct($resource);
        $this->data = $resource['attributes'];
    }



    public function toArray(Request $request): array
    {
        return [
            'name' => ResourceHelper::getValue($this->data['name'] ?? null, $this->country, $this->language),
            'image' => ResourceHelper::getImage($this->data['image'] ?? null),
            'icon' => ResourceHelper::getImage($this->data['icon'] ?? null),
            'sales_name' => ResourceHelper::getValue($this->data['sales_name'] ?? null, $this->country, $this->language),
            'sales_description' => ResourceHelper::getValue($this->data['sales_description'] ?? null, $this->country, $this->language),
            'sales_phone' => ResourceHelper::getValue($this->data['sales_phone'] ?? null, $this->country, $this->language),
            'sales_email' => ResourceHelper::getValue($this->data['sales_email'] ?? null, $this->country, $this->language),
            'sales_photo' => ResourceHelper::getImage($this->data['sales_photo'] ?? null),
            'regions' => RegionCollection::make($this->data['regions'])->language($this->language)->country($this->country),
        ];
    }

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