<?php

namespace App\Models;

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

/**
 * @package App\Models
 * @property int $id
 * @property int $payment_system_id
 * @property array $title
 * @property string $logo
 * @property string $slug
 * @property array $description
 *
 * @property PaymentSystemModel $paymentSystem
 *
 * @method getLogo(): string
 */
class PaymentSystemItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'payment_system_id',
        'title',
        'logo',
        'slug',
        'description'
    ];

    protected $casts = [
        'title' => 'array',
        'description' => 'array'
    ];

    protected $table = 'payment_system_items';

    public function paymentSystem(): BelongsTo
    {
        return $this->belongsTo(PaymentSystemModel::class, 'payment_system_id', 'id');
    }

    public function getTitle(): string
    {
        if (App::isLocale('ru')) {
            return (string) $this->title['ru'];
        }

        return (string) $this->title['uz'];
    }
    public function getDescription(): string
    {
        if ($this->description == null) {
            return '';
        }

        if (App::isLocale('ru')) {
            return (string) $this->description['ru'];
        }

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

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

            return env('APP_URL').'/'.$this->logo;
        }

        return '/images/nophoto.jpg';
    }
}
