<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Song extends Model
{
    use Sluggable;
    use FullTextSearch;
    
    protected $fillable = ['title','lyrics', 'album_id', 'artist_id'];

    protected $searchable = ['title','lyrics'] ;

    protected $dates = ['deleted_at'];

    public function artists() {
    	return $this->belongsToMany('App\Artist') ;
    }

    public static function DeleteSong($id) {
        $song            = Song::find($id) ;
        if (!$song) return;
        $artistID        = [] ;
        foreach ($song->artists()->get() as $artist) {
            $artistID[]  = $artist->id ;
        }
        $song->artists()->detach($artistID) ;
        $song->delete() ;
    }

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }
}
