<?php

namespace App\Repositories;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class LearningProduct extends Model implements HasMedia
{

	use SoftDeletes, InteractsWithMedia;

	protected $dates = ['deleted_at'];

	protected $guarded = ['id'];

	protected $appends = ['slug'];

    protected $casts = [
        'for_members' => 'boolean',
        'third_party_training' => 'boolean',
        'voor_opleiders' => 'boolean',
    ];

	public function getSlugAttribute()
	{
		return $this->id . '-' . Str::slug($this->title);
	}

	public function registerMediaCollections(): void
	{
		$this
			->addMediaCollection('learning_products_covers')
			->singleFile();

		$this
			->addMediaCollection('learning_products_tiles')
			->singleFile();
	}

	public function registerMediaConversions(Media $media = null): void
	{
		$this->addMediaConversion('thumb')
			->height(50);
	}

	public function draft()
	{
		return $this->hasOne('App\Repositories\LearningProduct', 'parent_id', 'id');
	}

	public function filters()
	{
		return $this->morphMany(FilterItemsAssociation::class, 'filter_items_associations');
	}

	public function versions()
	{
		return $this->hasMany(Version::class);
	}

	public function accreditations()
	{
		return $this->hasMany(Accreditation::class);
	}

	public function notifications()
	{
		return $this->hasMany(CourseNotification::class);
	}

	public function synonyms()
	{
		return $this->belongsToMany(Synonym::class);
	}
}