import test from 'node:test';
import assert from 'node:assert/strict';

process.env.NODE_ENV = 'test';
process.env.DB_PATH = ':memory:';

const jsonFetch = async (
  baseUrl: string,
  path: string,
  options?: { method?: string; body?: unknown },
) => {
  const res = await fetch(`${baseUrl}${path}`, {
    method: options?.method ?? 'GET',
    headers: options?.body ? { 'Content-Type': 'application/json' } : undefined,
    body: options?.body ? JSON.stringify(options.body) : undefined,
  });

  const text = await res.text();
  let json: any = null;
  try {
    json = text ? JSON.parse(text) : null;
  } catch {
    json = null;
  }
  return { status: res.status, json, text };
};

test('新用户创建后默认加入“全体用户”系统组（含 validate 自动创建与管理员导入）', async () => {
  const { initDatabase } = await import('../api/database');
  await initDatabase();

  const { app } = await import('../api/server');
  const server = app.listen(0);

  try {
    const addr = server.address();
    assert.ok(addr && typeof addr === 'object');
    const baseUrl = `http://127.0.0.1:${addr.port}`;

    // 1) validate 自动创建用户
    const created = await jsonFetch(baseUrl, '/api/users/validate', {
      method: 'POST',
      body: { name: '默认组用户', phone: '13800139001', password: '' },
    });
    assert.equal(created.status, 200);
    assert.equal(created.json?.success, true);
    const userId = created.json?.data?.id as string;
    assert.ok(userId);

    const list1 = await jsonFetch(baseUrl, '/api/admin/users?page=1&limit=20');
    assert.equal(list1.status, 200);
    assert.equal(list1.json?.success, true);
    const row1 = (list1.json?.data as any[]).find((u) => u.id === userId);
    assert.ok(row1);
    assert.ok(Array.isArray(row1.groups));
    assert.ok(row1.groups.some((g: any) => g.isSystem === 1 || g.isSystem === true));

    // 2) 管理员导入用户（模拟 Excel：走 importUsers 的解析逻辑不方便，这里直接调用 createUser 接口覆盖管理端创建路径）
    const createdAdmin = await jsonFetch(baseUrl, '/api/admin/users', {
      method: 'POST',
      body: { name: '管理员创建用户', phone: '13800139002', password: '', groupIds: [] },
    });
    assert.equal(createdAdmin.status, 200);
    assert.equal(createdAdmin.json?.success, true);
    const userId2 = createdAdmin.json?.data?.id as string;
    assert.ok(userId2);

    const list2 = await jsonFetch(baseUrl, '/api/admin/users?page=1&limit=50');
    assert.equal(list2.status, 200);
    assert.equal(list2.json?.success, true);
    const row2 = (list2.json?.data as any[]).find((u) => u.id === userId2);
    assert.ok(row2);
    assert.ok(Array.isArray(row2.groups));
    assert.ok(row2.groups.some((g: any) => g.isSystem === 1 || g.isSystem === true));
  } finally {
    server.close();
  }
});
