<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class GlobalConfigurationResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */

    public function toArray($request)
    {
        $competitionsTopAds = null;
        if ($this->competitions_top_ads_type == 'bar') {
            $competitionsTopAds = $this->competitionsTopAds()->active()->inRandomOrder()->limit(1)->get();
        } else {
            $competitionsTopAds = $this->competitionsTopAds()->active()->get();
        }

        $resultsTopAds = null;
        if ($this->results_top_ads_type == 'bar') {
            $resultsTopAds = $this->resultsTopAds()->active()->inRandomOrder()->limit(1)->get();
        } else {
            $resultsTopAds = $this->resultsTopAds()->active()->get();
        }

        $jobsTopAds = null;
        if ($this->jobs_top_ads_type == 'bar') {
            $jobsTopAds = $this->jobsTopAds()->active()->inRandomOrder()->limit(1)->get();
        } else {
            $jobsTopAds = $this->jobsTopAds()->active()->get();
        }

        $officeProfilesTopAds = null;
        if ($this->office_profiles_top_ads_type == 'bar') {
            $officeProfilesTopAds = $this->officeProfilesTopAds()->active()->inRandomOrder()->limit(1)->get();
        } else {
            $officeProfilesTopAds = $this->officeProfilesTopAds()->active()->get();
        }

        return [
            'competitionsTopAds' => AdvertisementResource::collection($competitionsTopAds),
            'competitionsTopAdsType' => $this->competitions_top_ads_type,
            'resultsTopAds' => AdvertisementResource::collection($resultsTopAds),
            'resultsTopAdsType' => $this->results_top_ads_type,
            'jobsTopAds' => AdvertisementResource::collection($jobsTopAds),
            'jobsTopAdsType' => $this->jobs_top_ads_type,
            'officeProfilesTopAds' => AdvertisementResource::collection($officeProfilesTopAds),
            'officeProfilesTopAdsType' => $this->office_profiles_top_ads_type,
            'facebookUrl' => $this->facebook_url,
            'instagramUrl' => $this->instagram_url,
        ];
    }
}
