/**
 * K6 Setup Verification Script
 * Run this to verify your k6 installation and environment
 */

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

// Test configuration - minimal load
export const options = {
  vus: 1,
  iterations: 1,
  thresholds: {
    http_req_duration: ['p(95) < 1000'], // Relaxed for verification
    http_req_failed: ['rate < 0.05'],     // Allow some failures during setup
  },
};

const baseUrl = __ENV.BASE_URL || 'http://localhost:3000';

export default function () {
  console.log(`=== K6 Setup Verification ===`);
  console.log(`Target: ${baseUrl}`);

  // Test 1: Server is reachable
  const homeRes = http.get(baseUrl);
  check(homeRes, {
    'Server is reachable': (r) => r.status !== 0,
    'Home page responds': (r) => [200, 301, 302, 404].includes(r.status),
  });
  console.log(`Home page status: ${homeRes.status}`);

  // Test 2: API endpoint exists
  const apiRes = http.get(`${baseUrl}/api/global`);
  check(apiRes, {
    'API endpoint responds': (r) => r.status !== 0,
  });
  console.log(`API status: ${apiRes.status}`);

  // Test 3: Check response times
  const timeRes = http.get(`${baseUrl}/api/pages`, {
    tags: { name: 'pages_api' },
  });
  console.log(`Pages API response time: ${timeRes.timings.duration}ms`);

  // Summary
  console.log(`=== Verification Complete ===`);
  console.log(`If all checks passed, you're ready to run load tests!`);
  console.log(`Next: k6 run tests/k6/public-browsing.js`);
}

export function setup() {
  console.log(`Starting K6 verification...`);
  console.log(`k6 version: ${__K6_VERSION__ || 'unknown'}`);
}

export function teardown(data) {
  console.log(`Verification finished.`);
}
