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

namespace App\Http\Resources\Pages\Career;

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;
use Illuminate\Support\Str;

/**
 * @OA\Schema(title="VacancyResource",description="Vacancy resource",
 *     @OA\Xml(name="VacancyResource")
 * )
 */
class VacancyResource extends JsonResource
{
    /**
     * @OA\Property(property="slug", type="string", example="chief_accountant"),
     * @OA\Property(property="title", type="string", example="Chief accountant"),
     * @OA\Property(property="title_background", type="string", example="https://uniferx-backend.test/posts/image.png"),
     * @OA\Property(property="locations", type="string", example="Ukraine, Kyiv"),
     * @OA\Property(property="experience", type="string", example="3 years of experience"),
     * @OA\Property(property="work_type", type="string", example="Office only"),
     * @OA\Property(property="data", type="string", example="<p>We are an international company that develops and implements innovative technologies</p>"),
     */
    public string|null $language;
    public string|null $country;
    public bool $full;
    public string|null $slug;
    public string|null $title;
    public array|string|null $title_image;
    public string|null $data;
    public string|null $preview_description;
    public string|null $fs_button;
    public string|null $locations;
    public string|null $experience;
    public string|null $work_type;
    public string|null $created_at;

    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);
        $this->slug = $resource->slug;
        $this->title = $resource->title;
        $this->title_image = $resource->title_image;
        $this->data = $resource->data;
        $this->locations = $resource->locations;
        $this->experience = $resource->experience;
        $this->work_type = $resource->work_type;
        $this->fs_button = $resource->fs_button;
        $this->created_at = $resource->created_at;
        $this->preview_description = $resource->preview_description;
        $this->country = $country;
        $this->language = $language;
        $this->full = $full;
    }



    public function toArray(Request $request): array
    {
        $data = [
            'slug' => $this->slug,
            'title' => ResourceHelper::getValue($this->title, $this->country, $this->language),
            'title_background' => ResourceHelper::getImage($this->title_image),
            'locations' => ResourceHelper::getValue($this->locations, $this->country, $this->language),
            'experience' => ResourceHelper::getValue($this->experience, $this->country, $this->language),
            'work_type' => ResourceHelper::getValue($this->work_type, $this->country, $this->language),
            'fs_button' => ResourceHelper::getValue($this->fs_button, $this->country, $this->language),
            'created_at' => Carbon::parse($this->created_at)->format('d.m.Y'),
        ];
        if (!$this->full) {
            $data['preview_description'] = ResourceHelper::getValue($this->preview_description, $this->country, $this->language);
        }
        if ($this->full) {
            $data['data'] = ResourceHelper::getValue($this->data, $this->country, $this->language);
        }

        return $data;
    }

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