<?php

namespace App\Models;

use App\Libs\Database\Eloquent\ContentTypeModel;
use App\Models\GenericBackoffice\Traits\LanguageSupportTrait;
use Illuminate\Support\Carbon;
use App\Traits\Indexable;
use App\Traits\FcmSubscribable;
use App\Traits\Favouritable;
use App\Collections\ExhibitionsCollection;
use App\Traits\WithShareUrl;



class Exhibition extends ContentTypeModel
{
    use LanguageSupportTrait;
    use Indexable;
    use FcmSubscribable;
    use Favouritable;
    use WithShareUrl;

    protected $hidden  = ['languages', "websites"];
    protected $with    = ['languages', "websites"];
    protected $dates   = []; // dates
    protected $casts = [
        'date_from' => 'datetime',
        'date_to'   => 'datetime',
    ];

    //the number of days an exhibition will be considered as ending before the end date
    public const DATE_STATUS_PAST          = 'past';
    public const DATE_STATUS_UPCOMING      = 'upcoming';
    public const DATE_STATUS_ONGOING       = 'ongoing';
    public const DATE_STATUS_ENDING        = 'ending';


    public function image()
    {
        return $this->belongsTo(MediaItem::class);
    }

    public function gifImage()
    {
        return $this->belongsTo(MediaItem::class);
    }

    public function video()
    {
        return $this->belongsTo(MediaItem::class);
    }

    public function roomSheetImage()
    {
        return $this->belongsTo(MediaItem::class);
    }

    public function location()
    {
        return $this->belongsTo(Location::class);
    }

    public function artists()
    {
        return $this->belongsToMany(Artist::class);
    }

    public function collections()
    {
        return $this->belongsToMany(Collection::class);
    }

    public function mediaGalleryItems()
    {
        return $this->hasMany(MediaGalleryItem::class);
    }

    /**
     * Get all of the tags for the exhibition.
     */
    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }


    /**
     * Get the exhibition date Status.
     *
     * @return string
     */
    public function getDateStatusAttribute(): string
    {
        if (time() < strtotime($this->date_from)) {
            return Exhibition::DATE_STATUS_UPCOMING;
        }

        $end  = Carbon::parse($this->date_to);
        $diff = $end->diffInDays(now());

        if ($diff < config('api.exhibit_ending_criteria')) {
            return Exhibition::DATE_STATUS_ENDING;
        }

        if (time() >= strtotime($this->date_from) && time() <= strtotime($this->date_to)) {
            return Exhibition::DATE_STATUS_ONGOING;
        }

        if (time() > strtotime($this->date_to)) {
            return Exhibition::DATE_STATUS_PAST;
        }

        return 'n/a';
    }


    /**
     * Gets the possible themes for an exhibition
     * @return array
     */
    public static function getThemes(): array
    {
        return [
            'light' => 'Light',
            'dark'  => 'Dark'
        ];
    }


    /**
     * Create a new Eloquent Collection instance.
     *
     * @param array $items
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function newCollection(array $items = [])
    {
        return new ExhibitionsCollection($items);
    }
}
