// HTTP API client using axios

import axios from 'axios';
import type { Event, Statistics, EventListResponse } from '../types';

const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000';

const apiClient = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
  },
  timeout: 10000,
});

// API functions

export const fetchEvents = async (
  page: number = 1,
  pageSize: number = 50,
  eventTypes?: string[]
): Promise<EventListResponse> => {
  const params: any = {
    skip: (page - 1) * pageSize,
    limit: pageSize,
  };

  if (eventTypes && eventTypes.length > 0) {
    params.event_type = eventTypes.join(',');
  }

  const response = await apiClient.get<EventListResponse>('/api/events', { params });
  return response.data;
};

export const fetchStatistics = async (): Promise<Statistics> => {
  const response = await apiClient.get<Statistics>('/api/statistics');
  return response.data;
};

export const resetStatistics = async (): Promise<void> => {
  await apiClient.delete('/api/events/reset');
};

export const checkHealth = async (): Promise<boolean> => {
  try {
    await apiClient.get('/health');
    return true;
  } catch {
    return false;
  }
};

export default apiClient;
