<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Widget extends Model
{
    protected $fillable = [
        'key',
        'image',
        'title',
        'content',
        'active',
    ];

    public static function getTitle(string $key): string
    {
        $widget = Cache::remember('widget-' . $key, 600, function () use ($key) {
            return Widget::query()
                ->where('key', $key)
                ->where('active', 1)
                ->first();
        });

        return $widget?->title ?? '';
    }

    public static function getContent(string $key): string
    {
        $widget = Cache::remember('widget-' . $key, 600, function () use ($key) { 
            return Widget::query()
                ->where('key', $key)
                ->where('active', 1)
                ->first();
        });

        return $widget?->content ?? '';
    }
    public static function getImage(string $key): string
    {
        $widget = Cache::remember('widget-' . $key, 600, function () use ($key) { 
            return Widget::query()
                ->where('key', $key)
                ->where('active', 1)
                ->first();
        });

        return $widget?->image ?? '';
    }
}
