/**
 * L2 回归测试：播放列表、草稿箱、视频生成、订阅支付
 * 批量覆盖模块 13-16
 */
import { test, expect } from '@playwright/test';
import { apiGet, apiPost, apiPut, apiDelete, initApiClient, disposeApiClient } from '../helpers/api-client';

// ===== 13: 播放列表 =====
test.describe('播放列表 (Playlists)', () => {
  test.beforeAll(async () => { await initApiClient(); });
  test.afterAll(async () => { await disposeApiClient(); });

  test('PL01: 获取播放列表 GET /api/playlists', async () => {
    const res = await apiGet('/api/playlists');
    expect(res.code).toBe(0);
  });

  test('PL02: 创建播放列表 POST /api/playlists', async () => {
    const res = await apiPost('/api/playlists', { name: '[自动化测试] 列表' });
    expect(res.code).toBe(0);
    const id = res.data?.id;
    if (id) {
      await apiDelete(`/api/playlists/${id}`).catch(() => {});
    }
  });

  test('PL03: 获取播放列表详情 GET /api/playlists/:id', async () => {
    const res = await apiGet('/api/playlists/1');
    expect([0, 404]).toContain(res.code);
  });
});

// ===== 14: 草稿箱 =====
test.describe('草稿箱 (Drafts)', () => {
  test.beforeAll(async () => { await initApiClient(); });
  test.afterAll(async () => { await disposeApiClient(); });

  test('DR01: 获取草稿列表 GET /api/drafts', async () => {
    const res = await apiGet('/api/drafts');
    expect(res.code).toBe(0);
  });

  test('DR02: 创建草稿 POST /api/drafts', async () => {
    const res = await apiPost('/api/drafts', {
      type: 'tts',
      data: { text: '测试', voiceId: 'Cherry' },
    });
    expect([0, 400, 404]).toContain(res.code);
  });

  test('DR03: 删除草稿 DELETE /api/drafts/:id', async () => {
    const res = await apiDelete('/api/drafts/99999');
    // 可能成功(0)、未找到(404)或需要认证(401)
    expect([0, 401, 404]).toContain(res.code);
  });
});

// ===== 15: 视频生成 =====
test.describe('视频生成 (VideoGenerator)', () => {
  test.beforeAll(async () => { await initApiClient(); });
  test.afterAll(async () => { await disposeApiClient(); });

  test('VG01: 获取视频项目列表 GET /api/video/projects', async () => {
    const res = await apiGet('/api/video/projects');
    // 可能成功(0)或需要认证(401)
    expect([0, 401]).toContain(res.code);
  });

  test('VG02: 创建视频项目 POST /api/video/projects', async () => {
    const res = await apiPost('/api/video/projects', { title: '[自动化测试] 视频' });
    // 可能成功(0)或需要认证(401)
    expect([0, 401]).toContain(res.code);
  });

  test('VG03: 获取素材列表 GET /api/video/materials', async () => {
    const res = await apiGet('/api/video/materials');
    // 可能成功(0)或需要认证(401)
    expect([0, 401]).toContain(res.code);
  });
});

// ===== 16: 订阅支付 =====
test.describe('订阅支付 (Subscription)', () => {
  test.beforeAll(async () => { await initApiClient(); });
  test.afterAll(async () => { await disposeApiClient(); });

  test('SU01: 获取套餐列表 GET /api/subscription/plans', async () => {
    const res = await apiGet('/api/subscription/plans');
    expect(res.code).toBe(0);
  });

  test('SU02: 获取余额 GET /api/subscription/balance', async () => {
    const res = await apiGet('/api/subscription/balance');
    expect(res.code).toBe(0);
  });

  test('SU03: 创建订单 POST /api/subscription/orders - 订单接口需支付流程，跳过', async () => {
    // /api/subscription/orders 路由不存在，跳过
    test.skip();
  });
});
