<?php

declare(strict_types=1);

namespace app\admin\controller\test;

use app\common\controller\Backend;
use app\common\library\admin\WebSocketConfigHelper;
use app\common\service\GameWebSocketPayloadHelper;
use support\Response;
use Webman\Http\Request as WebmanRequest;

/**
 * WebSocket test for period status stream.
 */
class GameCurrentStatus extends Backend
{
    protected ?object $model = null;

    public function wsConfig(WebmanRequest $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) {
            return $response;
        }

        $subscribeTopics = [
            'period.tick',
            'period.locked',
            'period.opened',
            'period.payout',
            'period.payout.tick',
            'bet.win',
            'user.streak',
            'wallet.changed',
            'bet.accepted',
            'jackpot.hit',
            'auto.spin.progress',
            'admin.live.snapshot',
            'admin.live.opened',
        ];

        $oddsPushTopics = GameWebSocketPayloadHelper::ODDS_PUSH_TOPICS;
        $testPlayerOdds = GameWebSocketPayloadHelper::adminTestPlayerOddsSnapshot();

        return $this->success('', [
            'name' => 'ws.period',
            'ws_url' => WebSocketConfigHelper::wsUrl($request),
            'connect_tip' => '连接成功后将自动订阅下列主题。真实业务仅在有玩家下注/结算时推送赔率；本页联调会在订阅后额外推送带 is_test/preview 的演示帧（见下方测试玩家赔率）。',
            'subscribe_topics' => $subscribeTopics,
            'odds_push_topics' => $oddsPushTopics,
            'player_odds_fields' => ['current_streak', 'streak_level', 'odds_factor', 'is_jackpot'],
            'test_player_odds' => $testPlayerOdds,
            'test_push_topics' => $oddsPushTopics,
            'sample_messages' => [
                '{"action":"ping"}',
                '{"action":"subscribe","topics":["period.tick","user.streak","period.opened","period.locked","period.payout"]}',
                '{"action":"subscribe","topics":["user.streak","wallet.changed","bet.accepted","auto.spin.progress"]}',
            ],
        ]);
    }
}

