<?php

namespace App\Collections;

use Illuminate\Database\Eloquent\Collection;

class ExhibitionsCollection extends Collection
{

    /**
     * Gets the current exhibitions from a collection
     * 
     * @param ExhibitionsCollection
     * @return ExhibitionsCollection
     */
    public function current(ExhibitionsCollection $items): ExhibitionsCollection
    {

        $endingCriteria = config('api.exhibit_ending_criteria'); //in days
        $ret            = $items->filter(function ($item) use ($endingCriteria) {
            return
                $item->date_to->diffInDays(now()) > $endingCriteria
                && $item->date_from < now()
                //&& $item->date_to > $endingCriteriaDate
                && $item->date_to > now();
        });

        return $ret->sortByDesc('highlight')
            ->sortByDesc('sponsored')
            ->sortBy('date_to');
    }


    /**
     * Gets the coming soon exhibitions from a collection
     * 
     * @param ExhibitionsCollection
     * @return ExhibitionsCollection
     */
    public function comingSoon($items)
    {
        $ret = $items->filter(function ($item) {
            return $item->date_from > now();
        });

        return $ret->sortBy('date_from');
    }

    /**
     * Gets the coming soon exhibitions from a collection
     *
     * @param ExhibitionsCollection
     * @return ExhibitionsCollection
     */
    public function comingSoonSponsored($items)
    {
        $ret = $items->filter(function ($item) {
            return $item->date_from > now();
        });


        return $ret->sortBy([
            ['sponsored', 'desc'],
            ['highlight', 'desc'],
            ['date_from', 'asc']
        ]);
    }




    /**
     * Gets the ending exhibitions from a collection
     * 
     * @param ExhibitionsCollection
     * @return ExhibitionsCollection
     */
    public function ending($items)
    {
        $endingCriteria = config('api.exhibit_ending_criteria'); //in days
        $ret            = $items->filter(function ($item) use ($endingCriteria) {
            return
                $item->date_to->diffInDays(now()) <= $endingCriteria
                && $item->date_from < now()
                //&& $item->date_to > $endingCriteriaDate
                && $item->date_to > now();
        });

        return $ret->sortByDesc('highlight')
            ->sortByDesc('sponsored')
            ->sortBy('date_to');
    }
}
