<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;

/**
 * Class Media
 *
 * @property int $id
 * @property uuid|null $uuid
 * @property int $media_collection_id
 * @property int|null $user_id
 * @property string $media_type
 * @property string $media_source
 * @property string $media_provider
 * @property string $name
 * @property string $mime_type
 * @property string $file_name
 * @property string $file_path
 * @property string $disk
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property MediaCollection $media_collection
 */
class Media extends Model
{
    use SoftDeletes;

    protected $table = 'medias';

    protected $casts = [
        'media_collection_id' => 'int',
        'user_id' => 'int',
    ];

    protected $fillable = [
        'uuid',
        'media_collection_id',
        'user_id',
        'media_type',
        'media_source',
        'media_provider',
        'mime_type',
        'file_name',
        'file_path',
        'disk',
        'uploaded_url',
    ];

    protected $appends = [
        'media_url',
    ];

    public function media_collection()
    {
        return $this->belongsTo(MediaCollection::class);
    }

    protected function mediaUrl(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                return Storage::disk($attributes['disk'])->url($attributes['file_path'].$attributes['file_name']);
            }
        );
    }
}
