<?php

namespace app\common\model;

use app\common\library\game\StreakWinReward;
use app\common\service\GameHotDataCoordinator;
use support\think\Model;

/**
 * 游戏/平台动态参数（KV）
 */
class GameConfig extends Model
{
    protected $name = 'game_config';

    protected $autoWriteTimestamp = true;

    protected $type = [
        'create_time' => 'integer',
        'update_time' => 'integer',
    ];

    public static function onAfterWrite(GameConfig $model): void
    {
        $key = trim((string) ($model->getAttr('config_key') ?? ''));
        if ($key !== '') {
            GameHotDataCoordinator::afterGameConfigKeyCommitted($key);
        }
        if ($key === StreakWinReward::CONFIG_KEY) {
            StreakWinReward::clearCache();
        }
    }

    public static function onAfterDelete(GameConfig $model): void
    {
        $key = trim((string) ($model->getAttr('config_key') ?? ''));
        if ($key !== '') {
            GameHotDataCoordinator::afterGameConfigKeyCommitted($key);
        }
        if ($key === StreakWinReward::CONFIG_KEY) {
            StreakWinReward::clearCache();
        }
    }
}
