<?php

namespace App\Models;

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

class IngameServiceSetting extends Model
{
    protected $fillable = ['key', 'value', 'description'];

    public static function get(string $key, $default = null)
    {
        return Cache::remember("ingame_setting_{$key}", 300, function () use ($key, $default) {
            $setting = self::where('key', $key)->first();
            return $setting ? $setting->value : $default;
        });
    }

    public static function set(string $key, $value): void
    {
        self::updateOrCreate(
            ['key' => $key],
            ['value' => $value]
        );
        Cache::forget("ingame_setting_{$key}");
    }

    public static function isEnabled(string $service): bool
    {
        return self::get("{$service}_enabled", '1') === '1';
    }

    public static function getPrice(string $service): int
    {
        return (int) self::get("{$service}_price", '0');
    }
}
