import http from 'k6/http';
import { check, sleep } from 'k6';

export let options = {
  stages: [
    { duration: '30s', target: 10 },  // ramp up to 10 users
    { duration: '1m', target: 10 },   // stay at 10 users
    { duration: '30s', target: 20 },  // ramp up to 20
    { duration: '1m', target: 20 },   // stay
    { duration: '30s', target: 0 },   // ramp down
  ],
  thresholds: {
    http_req_duration: ['p(95)<500'], // 95% of requests < 500ms
    http_req_failed: ['rate<0.01'],   // error rate < 1%
  },
};

const BASE_URL = 'http://localhost:9100';

export default function () {
  // Health check
  let res = http.get(`${BASE_URL}/health`);
  check(res, { 'health 200': (r) => r.status === 200 });

  // Admin endpoints (requires auth - we test 401 to ensure auth is enforced)
  res = http.get(`${BASE_URL}/api/admin/companies`);
  check(res, { 'companies 401': (r) => r.status === 401 });

  res = http.get(`${BASE_URL}/api/admin/users`);
  check(res, { 'users 401': (r) => r.status === 401 });

  // Public endpoints
  res = http.get(`${BASE_URL}/api/users/public`);
  check(res, { 'public users ok': (r) => r.status === 200 || r.status === 404 });

  sleep(1);
}
