import http from 'k6/http';
import { check, group } from 'k6';
import { Rate } from 'k6/metrics';

// Smoke test - quick verification that system works
export const options = {
  vus: 5,
  duration: '30s',
  thresholds: {
    http_req_duration: ['p(95)<500'],
    http_req_failed: ['rate<0.01'],
  },
};

const BASE_URL = __ENV.BASE_URL || 'http://localhost:8000';
const errorRate = new Rate('errors');

export default function() {
  group('Smoke Test - Core Endpoints', () => {
    // Health check
    const health = http.get(`${BASE_URL}/health`);
    const healthCheck = check(health, {
      'health status is 200': (r) => r.status === 200,
      'health response time < 200ms': (r) => r.timings.duration < 200,
    });
    errorRate.add(!healthCheck);
    
    // API docs available
    const docs = http.get(`${BASE_URL}/docs`);
    const docsCheck = check(docs, {
      'docs status is 200': (r) => r.status === 200,
    });
    errorRate.add(!docsCheck);
    
    // OpenAPI schema
    const openapi = http.get(`${BASE_URL}/openapi.json`);
    const openapiCheck = check(openapi, {
      'openapi status is 200': (r) => r.status === 200,
      'openapi has paths': (r) => r.json('paths') !== undefined,
    });
    errorRate.add(!openapiCheck);
  });
  
  group('Smoke Test - API v1', () => {
    const API_V1 = `${BASE_URL}/api/v1`;
    
    // Public endpoints
    const scenarios = http.get(`${API_V1}/scenarios`);
    check(scenarios, {
      'scenarios endpoint responds': (r) => r.status !== 0,
    });
    
    // Authentication endpoint
    const login = http.post(`${API_V1}/auth/login`, JSON.stringify({
      username: 'test@test.com',
      password: 'test',
    }), {
      headers: { 'Content-Type': 'application/json' },
    });
    check(login, {
      'auth endpoint responds': (r) => r.status !== 0,
    });
  });
}
