/**
 * E2E：开奖 + 结算 + 派彩（确定性流水线，无 skip）
 */

import { test, expect, request as pwRequest } from '@playwright/test';
import {
  playerLoginViaBypass,
  resetE2EPlayer,
  fetchCurrentDrawNo,
} from './_helper';
import { runWalletDrawSettlement } from './helpers/draw-settlement';

test('下注 → 关盘 → 录号 → publish → 结算 → 派彩 → 余额涨 + 公开结果可查', async () => {
  test.setTimeout(180_000);

  await resetE2EPlayer();
  const draw = await fetchCurrentDrawNo();
  expect(draw?.draw_no, '当前没有 open 期号').toBeTruthy();

  const session = await playerLoginViaBypass();
  const adminSession = await (await import('./_helper')).adminLoginViaBypass();

  const result = await runWalletDrawSettlement({
    adminToken: adminSession.accessToken,
    playerToken: session.access_token,
    drawNo: draw!.draw_no,
  });

  expect(result.balanceAfter).toBeGreaterThan(result.balanceBefore);

  const publicCtx = await pwRequest.newContext();
  const pub2 = await publicCtx.get(`/api/v1/draw/results/${draw!.draw_no}`);
  expect(pub2.ok()).toBeTruthy();
  const pb2 = (await pub2.json()) as any;
  expect(pb2.code).toBe(0);
  await publicCtx.dispose();
});
