<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Storage;

/**
 * @package App\Models
 * @property int $id
 * @property array $name
 * @property string $image
 * @property int $position
 * @property string $created_at
 * @property string $updated_at
 *
 * @property UsefulInfoItem[] $items
 *
 */
class UsefulInfo extends Model
{
    use HasFactory;

    protected $table = 'useful_infos';

    protected $fillable = [
        'name',
        'image',
        'position'
    ];

    protected $casts = [
        'name' => 'array',
    ];

    public function items()
    {
        return $this->hasMany(UsefulInfoItem::class);
    }

    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 getName(): string
    {
        if (App::isLocale('ru')) {
            return (string) $this->name['ru'];
        }

        return (string) $this->name['uz'];
    }
}
