<?php

namespace App\Packages\File\Models;

use App\Packages\Core\Traits\RepositoryTrait;
use App\Packages\File\Repository\FileUploadedRepository;
use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;

/**
 * Class FileUploaded
 * @package App\Packages\File\Models
 *
 * @property int $id
 * @property string $uuid
 * @property string $filename
 * @property string $mime_type
 * @property string $size
 * @property string $hash
 * @property string $ext
 * @property string $local_folder
 * @property string $do_key
 * @property string $do_bucket
 *
 * @property string $created_at
 * @property string $updated_at
 */
class FileUploaded extends Model
{
	use RepositoryTrait;

	/** @var string */
	public static $repositoryClass = FileUploadedRepository::class;

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

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

	/**
	 * @param $fileContents
	 * @param $extension
	 * @param $mimeType
	 * @param $size
	 *
	 * @return FileUploaded
	 */
	public static function createFileData($fileContents, $extension, $mimeType, $size)
	{
		$fileUploaded = new FileUploaded();
		$fileUploaded->hash = md5($fileContents);
		$fileUploaded->ext = $extension;
		$fileUploaded->mime_type = $mimeType;
		$fileUploaded->size = $size;
		$fileUploaded->save();

		return $fileUploaded;
	}

	/**
	 * @return string
	 */
	public function getFileLocalUrl()
	{
		return '/uploads/' . $this->getFullPath();
	}

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

	/**
	 * @return string
	 */
	public function getFolderPath()
	{
		return (string)ceil($this->id / 1000);
	}

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

	public function getFileSpacesUrl()
	{
		return '';
	}
}
