<?php

namespace App\Packages\User\Models;

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

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

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

	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
     * @param $width
     * @param $height
     *
     * @return UserAvatar
     */
    public static function createFileData($fileHash, $extension, $mimeType, $size, $width, $height)
    {
        $userAvatar = new UserAvatar();
        $userAvatar->hash = $fileHash;
        $userAvatar->ext = $extension;
        $userAvatar->mime_type = $mimeType;
        $userAvatar->size = $size;
        $userAvatar->width = $width;
        $userAvatar->height = $height;
        $userAvatar->save();

        return $userAvatar;
    }

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

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

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