<?php

namespace App\Models;

use App\Traits\LogOptionsTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Traits\LogsActivity;

class Slider extends Model
{
    use LogsActivity, LogOptionsTrait;

    protected $fillable = [
        'name',
        'image',
        'language',
        'link',
        'type',
        'views',
        'placement',
        'published',
        'position'
    ];

    protected $casts = [
        'published' => 'boolean'
    ];

    protected static $logName = 'sliders';
    protected static $logOnlyDirty = true;
    protected static $submitEmptyLogs = false;
    protected static $logAttributes = [
        'name',
        'image',
        'language',
        'link',
        'type',
        'placement',
        'published',
        'position'
    ];

    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 $this->image;
        }

        return '/images/nophoto.jpg';
    }

    public function scopePublished($query)
    {
        return $query->where('published', true);
    }
}
