<?php

namespace App\Models;

use App\Services\ConfigService;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;

class Config extends BaseModel
{
    protected $table = 'config';
    protected $fillable = ['field', 'val', 'remark', 'group_id'];

    /*
     * group_id
     * 1 系统基础配置
     * 2 频道消息配置
     * 3 用户自定义配置
     * 4 极速PC28相关配置
     *
     *
     */

    public static function setPc28Switch(): void
    {
        $pc28_switch = Cache::get('pc28_switch', 36);
        $pc28_switch = intval($pc28_switch);
        $val = static::where('field', 'pc28_switch')->first()->val;
        $val = intval($val);
        if (!in_array($pc28_switch,[0,1])) $pc28_switch = $val;
        if ($pc28_switch !== $val) {
            static::where('field', 'pc28_switch')->update(['val' => $pc28_switch]);
            $lang = App::getLocale();
            $group_language = static::where('field', 'group_language')->first()->val;
            App::setLocale($group_language);
            $groupText = "------" . lang("已切换为极速PC28") . "------";
            if ($pc28_switch == 0) $groupText = "-----" . lang("已切换为加拿大PC28") . "------";
            App::setLocale($lang);
            ConfigService::asyncBettingGroupNotice($groupText, isTop: true);   // 异步群通知
        }
    }
}
