<?php

namespace App\Models;

use App\Traits\LogOptionsTrait;
use Illuminate\Support\Facades\App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Traits\LogsActivity;

/**
 * Class Brend
 * @property string|null $name
 * @property string|null $image
 * @property integer|null $id
 * @mixin Model
 */
class Brand extends Model
{
    use LogsActivity, LogOptionsTrait;

    /**
     * @var array
     */
    protected $fillable = [
        'name',
        'image',
        'slug',
        "position",
    ];

    /**
     * @var array
     */
    protected $casts = [
        'name' => 'array',
        'image' => 'string'
    ];

    protected static $logName = 'brand';
    protected static $logAttributes = ['name'];
    protected static $submitEmptyLogs = false;
    protected static $logOnlyDirty = true;

    /**
     * @return int
     */
    public function getID(): int
    {
        return (int) $this->id;
    }

    public function getName()
    {
        return $this->name[App::getLocale()] ?? null;
    }

    /**
     * @return string
     */
    public function getImage(): string
    {
        if (!empty($this->image)) {
            if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio'])) {
                return Storage::temporaryUrl(
                    $this->image,
                    Date::now()->addMinutes(5)
                );
            };

            return (string) '/' . $this->image;
        }

        return (string) 'images/no_brend.png';
    }

    public function categories()
    {
        return $this->belongsToMany(Category::class, 'categories_brands');
    }
}
