<?php

namespace Modules\Banner\Models;

use App\Services\Upload\UploadService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class Banner extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'image',
    ];

    public function currentDescription(): HasOne
    {
        return $this->hasOne(BannerDescription::class)->where('language_id', config('current_language_id'));
    }

    public function setImageAttribute($value): void
    {
        if (! $value) {
            return;
        }

        if (is_string($value)) {
            $this->attributes['image'] = $value;
            return;
        }

        $this->attributes['image'] = UploadService::upload($value, $this->attributes['image'] ?? null, 'banners');
    }

    public function getImageUrlAttribute(): ?string
    {
        return UploadService::url($this->image);
    }
}
