/**
 * Shared K6 configuration and helpers for Goodgo load tests.
 */

export const BASE_URL = __ENV.API_BASE_URL || 'http://localhost:3001';

/** Standard SLA thresholds — all endpoints must meet these. */
export const SLA_THRESHOLDS = {
  http_req_duration: ['p(50)<200', 'p(95)<500', 'p(99)<1000'],
  http_req_failed: ['rate<0.01'], // <1% error rate
};

/** Generate a unique phone number for test users. */
export function uniquePhone() {
  const suffix = String(Date.now()).slice(-8);
  return `09${suffix}`;
}

/** Register a test user and return { accessToken, refreshToken }. */
export function registerTestUser(http, suffix) {
  const phone = suffix || uniquePhone();
  const payload = JSON.stringify({
    phone,
    password: 'LoadTest@1234!',
    fullName: `K6 User ${phone}`,
    email: `k6-${phone}@goodgo.test`,
  });

  const res = http.post(`${BASE_URL}/auth/register`, payload, {
    headers: { 'Content-Type': 'application/json' },
    tags: { name: 'setup_register' },
  });

  if (res.status === 201 || res.status === 200) {
    return JSON.parse(res.body);
  }

  // User may already exist — try login
  const loginRes = http.post(
    `${BASE_URL}/auth/login`,
    JSON.stringify({ phone, password: 'LoadTest@1234!' }),
    {
      headers: { 'Content-Type': 'application/json' },
      tags: { name: 'setup_login' },
    },
  );

  if (loginRes.status === 200 || loginRes.status === 201) {
    return JSON.parse(loginRes.body);
  }

  return null;
}

/** Build auth headers from an access token. */
export function authHeaders(accessToken) {
  return {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${accessToken}`,
  };
}
