import http from 'k6/http';
import { check, group } from 'k6';
import { Rate, Trend } from 'k6/metrics';
import { getBaseUrl, getTargetRps, getDuration, defaultThresholds, checkResponse, randomString } from '../lib/common.js';

const enrollmentLatency = new Trend('enrollment_p99');
const verificationLatency = new Trend('verification_p99');
const modelLatency = new Trend('model_retrieval_p99');

const TARGET_RPS = getTargetRps();
const DURATION = getDuration();

export const options = {
  scenarios: {
    sustained_load: {
      executor: 'constant-arrival-rate',
      duration: DURATION,
      rate: TARGET_RPS,
      preAllocatedVUs: 20,
      maxVUs: 100,
      startTime: '0s',
      exec: 'default',
      tags: { scenario: 'sustained_load' },
    },
  },
  thresholds: {
    ...defaultThresholds(250).thresholds,
    enrollment_p99: ['p(99)<500'],
    verification_p99: ['p(99)<250'],
    model_retrieval_p99: ['p(99)<100'],
  },
};

const BASE_URL = getBaseUrl();
const AUTH_TOKEN = __ENV.API_TOKEN || '';

const headers = {
  'Content-Type': 'application/json',
  'Authorization': `Bearer ${AUTH_TOKEN}`,
};

export default function () {
  group('Voiceprint Enrollment', function () {
    const payload = JSON.stringify({
      userId: `loadtest-${randomString()}`,
      audioSample: 'base64-encoded-audio-data-placeholder',
      sampleRate: 16000,
    });
    const res = http.post(`${BASE_URL}/voiceprint/enroll`, payload, { headers });
    checkResponse(res, 200);
    enrollmentLatency.add(res.timings.duration);
  });

  group('Voiceprint Verification', function () {
    const payload = JSON.stringify({
      userId: `loadtest-${randomString()}`,
      audioSample: 'base64-encoded-audio-data-placeholder',
      sampleRate: 16000,
    });
    const res = http.post(`${BASE_URL}/voiceprint/verify`, payload, { headers });
    checkResponse(res, 200);
    verificationLatency.add(res.timings.duration);
  });

  group('Model Retrieval', function () {
    const res = http.get(`${BASE_URL}/voiceprint/model/${randomString()}`, { headers });
    checkResponse(res, 200);
    modelLatency.add(res.timings.duration);
  });
}
