<?php

namespace App\Models\Polymorphs;

use App\Models\Traits\HasDates;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\MediaLibrary\MediaCollections\Models\Media as SpatieMediaModel;

class Media extends SpatieMediaModel{
    use HasFactory,HasDates;

    protected $guarded = ['id','created_at'];
    protected $table= "medias";
    const MORPH_ID_COLUMN = "model_id";
    const MORPH_TYPE_COLUMN = "model_type";
    const MORPH_FUNCTION_NAME = "mediable";
    const FK_ID = "media_id";
    const DISK_NAME = "medias";


    //Relations
    public function mediable(){
        return $this->morphTo(
            __FUNCTION__,
            self::MORPH_TYPE_COLUMN,
            self::MORPH_ID_COLUMN
        );
    }

    //Getters
//    public function getMediaTypeAttribute(){
//        $type = __("non spécifié");
//        if ($this->isAudio()){
//            $type = __("audio");
//        }elseif ($this->isImage()){
//            $type = __("image");
//        }elseif ($this->isVideo()){
//            $type = __("video");
//        }elseif ($this->isDocument()){
//            $type = __("document");
//        }
//        return $type;
//    }

    public function getUrlAttribute(){
          return $this->getPath();
    }

    //Functions
    /*determine si le media est un audio*/
//    public function isAudio(){
//        //return ($this->media_type_id == MediaType::AUDIO);
//        return in_array($this->mime_type , MimeType::audios());
//    }
//
//    /*determine si le media est une video*/
//    public function isVideo(){
//        return in_array($this->mime_type , MimeType::videos());
//    }
//
//    /*determine si le media est une image*/
//    public function isImage(){
//        return in_array($this->mime_type , MimeType::images());
//    }
//
//    /*determine si le media est un audio*/
//    public function isDocument(){
//        return in_array($this->mime_type , MimeType::documents());
//    }
}
