<?php

namespace App\Models;

use App\Observers\WorkoutObserver;
use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class Workout extends Model implements TranslatableContract
{
    use Translatable;

    protected $guarded         = ['id', 'created_at', 'updated_at'];
    public $translatedAttributes = ['title', 'desc'];

    protected static function boot()
    {
        parent::boot();
        Workout::observe(WorkoutObserver::class);
    }

    public function getImageAttribute($key)
    {
        $media = $this->media()->where('option', 'image')->first();
        $image = $media ? asset('storage/images/workouts/' . $media->media) : null;
        return $image;
    }

    public function getVideoAttribute($key)
    {
        $media = $this->media()->where('option', 'video')->first();
        $video = $media ? asset('storage/files/workouts/' . $media->media) : null;
        return $video ;
    }

    // Relations
    // ========================= Image ===================
    public function media()
    {
    	return $this->morphOne(AppMedia::class,'app_mediaable');
    }

    public function category()
    {
        return $this->belongsTo(WorkoutCategory::class, 'workout_category_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    

}
