/**
 * 注册测试账号脚本
 *
 * 用途：快速注册测试账号，方便开发和测试
 * 运行：node scripts/register-test-user.js
 */

const TEST_CREDENTIALS = {
  email: "mnote.e2e@example.com",
  password: "MnoteE2E123!",
  name: "mnote-e2e",
};

async function registerTestUser() {
  const baseUrl = "http://localhost:3000";

  console.log("正在注册测试账号...");
  console.log(`邮箱: ${TEST_CREDENTIALS.email}`);
  console.log(`密码: ${TEST_CREDENTIALS.password}`);

  try {
    const response = await fetch(`${baseUrl}/api/auth`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        action: "auth:signIn",
        args: {
          provider: "password",
          params: {
            email: TEST_CREDENTIALS.email,
            password: TEST_CREDENTIALS.password,
            name: TEST_CREDENTIALS.name,
            flow: "signUp",
          },
        },
      }),
    });

    const result = await response.json();

    if (response.ok) {
      console.log("✓ 测试账号注册成功！");
      console.log(`\n您现在可以使用以下凭据登录：`);
      console.log(`  邮箱: ${TEST_CREDENTIALS.email}`);
      console.log(`  密码: ${TEST_CREDENTIALS.password}`);
      console.log(`\n或在登录页面点击"测试账号快速登录"按钮。`);
    } else {
      const message = result?.error || result?.message || "";
      if (/already exists|已存在|已被使用|已被占用/i.test(message)) {
        console.log("ℹ 测试账号已存在，可直接使用：");
        console.log(`  邮箱: ${TEST_CREDENTIALS.email}`);
        console.log(`  密码: ${TEST_CREDENTIALS.password}`);
        return;
      }
      console.log("ℹ 自动注册失败，请通过浏览器手动注册：");
      console.log(`  1. 访问 http://localhost:3000/auth`);
      console.log(`  2. 点击"测试账号快速登录"或"还没有账户？立即注册"`);
      console.log(`  3. 填写：`);
      console.log(`     邮箱: ${TEST_CREDENTIALS.email}`);
      console.log(`     密码: ${TEST_CREDENTIALS.password}`);
      console.log(`     姓名: ${TEST_CREDENTIALS.name}`);
    }
  } catch (error) {
    console.error("✗ 请求失败:", error.message);
    console.log("\n请确保开发服务器正在运行 (pnpm dev)");
  }
}

registerTestUser();
