<?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 string $status
 * @property string $type
 * @property int $position
 * @property bool $is_power
 * @property bool $with_problem
 *
 * @method getImage(): string
 * @method getName(): string
 *
 * @property Status $getStatus
 */
class Service extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'image',
        'status',
        'type',
        'is_power',
        'position',
        'with_problem'
    ];

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

    public function getStatus()
    {
        return $this->belongsTo(Status::class, 'status', 'slug');
    }

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

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

    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 problems()
    {
        return $this->belongsToMany(Problem::class, 'service_problems');
    }
}
