/**
 * E2E 冒烟：健康检查 + 公开 draw/captcha/玩家 ping。
 *
 * 不依赖任何账号，跑通说明：
 *   - docker compose 起得起来
 *   - php artisan serve 真的在 127.0.0.1:8000
 *   - PG / Redis / 缓存 / 路由都活着
 */

import { test, expect, request as pwRequest } from '@playwright/test';

const API = process.env.PLAYWRIGHT_API_URL ?? 'http://127.0.0.1:8000';

test('GET /api/v1/health 返回 200', async () => {
  const ctx = await pwRequest.newContext();
  const r = await ctx.get('/api/v1/health');
  expect(r.ok(), `status=${r.status()}`).toBeTruthy();
  await ctx.dispose();
});

test('GET /api/v1/player/ping 返回 200', async () => {
  const ctx = await pwRequest.newContext();
  const r = await ctx.get('/api/v1/player/ping');
  expect(r.ok(), `status=${r.status()}`).toBeTruthy();
  await ctx.dispose();
});

test('GET /api/v1/draw/current 不要求登录且包含 draw_no', async () => {
  const ctx = await pwRequest.newContext();
  const r = await ctx.get('/api/v1/draw/current');
  expect(r.ok(), `status=${r.status()}`).toBeTruthy();
  const body = (await r.json()) as any;
  // data 是 DrawHallSnapshot，draw_no 不一定有（如果没"当前可下注"期号就 null）
  expect(body).toHaveProperty('data');
  await ctx.dispose();
});

test('GET /api/v1/player/auth/captcha 返回 base64 + uuid key', async () => {
  const ctx = await pwRequest.newContext();
  const r = await ctx.get('/api/v1/player/auth/captcha');
  expect(r.ok(), `status=${r.status()}`).toBeTruthy();
  const body = (await r.json()) as any;
  expect(body.data.captcha_key).toMatch(/^[0-9a-f-]{36}$/);
  expect(body.data.image_base64).toBeTruthy();
  const svg = Buffer.from(body.data.image_base64, 'base64').toString('utf8');
  expect(svg).toContain('<svg');
  await ctx.dispose();
});
