<?php

namespace Modules\Partner\Models;

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

class Partner extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'image','logo'
    ];


    public function photos()
    {
        return $this->hasMany(PartnerPhoto::class);
    }
    /**
     * Get current description of the the Room
     * @return HasOne
     */
    public function currentDescription() :HasOne
    {
        return $this->hasOne(PartnerDescription::class)->where('language_id', config('current_language_id'));
    }

    public function setImageAttribute($value): void
    {
        if ($value) {
            if (is_string($value)) {
                $this->attributes['image'] = $value;
            } else {
                $oldImage = request('old_image') ?? $this->image ?? null;
                $image = UploadService::upload($value, $oldImage, 'partners');
                $this->attributes['image'] = $image;
            }
        } elseif ($this->image ?? false) {
            $this->attributes['image'] = $this->image;
        }
    }

    public function setLogoAttribute($value): void
    {
        if ($value) {
            if (is_string($value)) {
                $this->attributes['logo'] = $value;
            } else {
                $oldLogo = request('old_logo') ?? $this->logo ?? null;
                $logo = UploadService::upload($value, $oldLogo, 'partners');
                $this->attributes['logo'] = $logo;
            }
        } elseif ($this->logo ?? false) {
            $this->attributes['logo'] = $this->logo;
        }
    }

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

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