<?php

namespace App\Packages\Capsule\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Ramsey\Uuid\Uuid;

/**
 * Class CapsuleTeaser
 * @package App\Packages\Capsule\Models
 *
 * @property int $id
 * @property string $uuid
 * @property string $size
 * @property string $hash
 * @property string $ext
 * @property string $mime_type
 * @property string $aws_key
 * @property string $aws_bucket
 *
 * @property int $duration
 *
 * @property string|Carbon $created_at
 * @property string|Carbon $updated_at
 * @property string|Carbon $deleted_at
 */
class CapsuleTeaser extends Model
{
    use SoftDeletes;

	/** @var string */
	protected $table = 'table_capsule_teaser';

	public static function boot()
	{
		parent::boot();
		static::creating(function ($model) {
			/** @var static $model */
			$model->uuid = Uuid::uuid4();
		});
	}

	/**
	 * @param string $fileHash
	 * @param string $extension
	 * @param string $mimeType
	 * @param string $size
	 *
	 * @return CapsuleTeaser
	 */
	public static function createFileData($fileHash = '', $extension = '', $mimeType = '', $size = '')
	{
		$capsuleVideo = new CapsuleTeaser();
		$capsuleVideo->hash = $fileHash;
		$capsuleVideo->ext = $extension;
		$capsuleVideo->mime_type = $mimeType;
		$capsuleVideo->size = $size;
		$capsuleVideo->save();

		return $capsuleVideo;
	}

    /**
     * @return string
     *
     * @throws \Exception
     */
	public function generateName()
	{
		return Uuid::uuid4() . "." . $this->ext;
	}

	/**
	 * Get full file path
	 */
	public function getAwsFullPath()
	{
		return env('AWS_URL') . '/' . env('AWS_VIDEO_BUCKET') . '/' . $this->aws_key;
	}

	/**
	 * @return string
	 */
	public function getFullFileName()
	{
		return $this->uuid . '.' . $this->ext;
	}
}
