<?php

namespace App\Http\Resources;

use Illuminate\Support\Arr;
use Illuminate\Http\Resources\Json\JsonResource;

class LearningProductResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $filtersGrouped = [];

        // merging accreditation filters to allow the filtering in frontend
        if (isset($this->accreditations)) {

            foreach ($this->accreditations as $accreditation) {

                foreach ($accreditation->filters as $filter) {

                    $filtersGrouped[$filter->filter_item->filter_id][] = $filter->filter_item_id;
                    // To avoid double values
                    $filtersGrouped[$filter->filter_item->filter_id] = array_unique($filtersGrouped[$filter->filter_item->filter_id]);

                }
            }
        }


        if (isset($this->filters)) {

            foreach ($this->filters as $filter) {

                $filtersGrouped[$filter->filter_item->filter_id][] = $filter->filter_item_id;
                // To avoid double values
                $filtersGrouped[$filter->filter_item->filter_id] = array_unique($filtersGrouped[$filter->filter_item->filter_id]);

            }
        }

        $filtersFlattened = [
            'status' => null,
            'product_type' => null,
            'theme' => null,
            'course' => null,
        ];

        foreach ($filtersFlattened as $title => $value) {

            if (isset($this->filters)) {
                foreach ($this->filters as $filter) {
                    if (
                        $filter->filter_item->filter->title === $title
                        && $filter->filter_item->title
                    ) {
                        $filtersFlattened[$title][] = $filter->filter_item->title;
                    }
                }
                if ($filtersFlattened[$title]) {
                    sort($filtersFlattened[$title]);
                    $filtersFlattened[$title] = implode(", ", $filtersFlattened[$title]);
                }
            }
        }

        return [
            'id' => $this->id,
            'parent_id' => $this->parent_id,
            'title' => $this->title,
            'description' => $this->description,
            'video' => $this->video,
            'code' => $this->code,
            'published' => $this->published,
            'lead_time' => $this->lead_time,
            'short_description' => $this->short_description,
            'learning_goals' => $this->learning_goals,
            'review' => $this->review,
            'certification' => $this->certification,
            'extra_information' => $this->extra_information,
            'target_audience' => $this->target_audience,
            'quality_standards' => $this->quality_standards,
            'in_the_picture' => $this->in_the_picture,
            'in_the_picture_start' => $this->in_the_picture_start,
            'in_the_picture_end' => $this->in_the_picture_end,
            'owner' => $this->owner,
            'partner' => $this->partner,
            'supplier' => $this->supplier,
            'contract_agreements' => $this->contract_agreements,
            'draft' => $this->draft ? $this->draft : null,
            'slug' => $this->slug,
            'seo_title' => $this->seo_title,
            'meta_description' => $this->meta_description,
            'url' => $this->url,
            'cover' =>  [
                'full' => $this->getFirstMediaUrl('learning_products_covers'),
                'thumb' => $this->getFirstMediaUrl('learning_products_covers', 'thumb')
            ],
            'tile' =>  [
                'full' => $this->getFirstMediaUrl('learning_products_tiles'),
                'thumb' => $this->getFirstMediaUrl('learning_products_tiles', 'thumb')
            ],
            'prognosis_members' => $this->prognosis_members,
            'prognosis_attendees' => $this->prognosis_attendees,
            'sharepoint_link' => $this->sharepoint_link,
            'support_link' => $this->support_link,
            'support_tickets_link' => $this->support_tickets_link,
            'filters' => $this->whenLoaded('filters'),
            'accreditations' => $this->whenLoaded('accreditations'),
            'notifications' => $this->whenLoaded('notifications'),
            'filtersGrouped' => (object) $filtersGrouped,
            'filterItemsSelected' => Arr::flatten(get_object_vars((object) $filtersGrouped)),
            'versions' => $this->whenLoaded('versions'),
            'synonyms' => $this->synonyms,
            'for_members' => $this->for_members,
            'third_party_training' => $this->third_party_training,
            'voor_opleiders' => $this->voor_opleiders,

            // Returns a concatenated string with all the "theme" titles attached
            'theme' => $filtersFlattened['theme'],
            'status' => $filtersFlattened['status'],
            'course' => $filtersFlattened['course'],
            'product_type' => $filtersFlattened['product_type'],

            'created_at' => $this->created_at->toDateTimeString(),
            'updated_at' => $this->updated_at->toDateTimeString(),
            'deleted_at' => $this->deleted_at ? $this->deleted_at->toDateTimeString() : null,
        ];
    }
}
