<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ProductImage
 *
 * @property int $id
 * @property int|null $product_id
 * @property string $filename
 * @property string $original_name
 * @property string $ext
 * @property string $mine
 * @property int $size
 * @property int $active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Product|null $product
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereExt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereFilename($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereMine($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereOriginalName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereSize($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereUpdatedAt($value)
 * @property string|null $slug
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage findSimilarSlugs($attribute, $config, $slug)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage query()
 * @property int|null $pos
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductImage wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductImage withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug)
 * @property int|null $user_wl_product_id
 * @property string|null $type
 * @property object|null $attributes
 * @property-read \App\Models\UserWhitelabelProduct|null $user_wl_product
 * @method static \Illuminate\Database\Eloquent\Builder|ProductImage whereAttributes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductImage whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductImage whereUserWlProductId($value)
 * @mixin \Eloquent
 */
class ProductImage extends Model
{
    use Sluggable;


    protected $table = 'product_images';

    protected $casts = [
        'attributes' => 'object'
    ];

    protected $fillable = [
        'product_id', 'user_wl_product_id', 'type', 'filename', 'original_name', 'ext', 'mine', 'size', 'attributes'
    ];

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'original_name'
            ]
        ];
    }

    public function product()
    {
        return $this->belongsTo('App\Models\Product', 'product_id');
    }

    public function user_wl_product()
    {
        return $this->belongsTo('App\Models\UserWhitelabelProduct', 'user_wl_product_id');
    }

    public function formatBytes($precision = 2)
    {
        $size = $this->size;

        if ($size > 0) {
            $size = (int) $size;
            $base = log($size) / log(1024);
            $suffixes = array(' bytes', ' KB', ' MB', ' GB', ' TB');

            return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
        } else {
            return $size;
        }
    }

    public function getImagePath()
    {   
        if($this->type === 'uwllogo'){
            return '/images/user_product/'.$this->user_wl_product_id .'/'.$this->filename;
        }
        if($this->type === 'product'){
            return '/images/product/'.$this->product_id .'/'.$this->filename;
        }
        if($this->type === 'wllogo'){
            return '/images/product/'.$this->product_id .'/'.$this->filename;
        }

        return '/images/product/'.$this->product_id .'/'.$this->filename;
    }


    public function getBaseImagePath(){
            return base_path()."/storage/app/public".$this->getImagePath();
    }
}
