import axios from 'axios';

const API_URL = `http://${window.location.hostname}:3000/api`;

const api = axios.create({
  baseURL: API_URL,
});

export const authService = {
  checkStatus: () => api.get('/auth/status'),
  setup: (data) => api.post('/auth/setup', data),
  login: (credentials) => api.post('/auth/login', credentials),
};

export const positionService = {
  getAll: () => api.get('/positions'),
  create: (data) => api.post('/positions', data),
  delete: (id) => api.delete(`/positions/${id}`),
};

export const candidateService = {
  getAll: () => api.get('/candidates'),
  create: (formData) => api.post('/candidates', formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  }),
  delete: (id) => api.delete(`/candidates/${id}`),
};

export const voteService = {
  checkAdmission: (num) => api.get(`/vote/check/${num}`),
  castVote: (voteData) => api.post('/vote', voteData),
  getResults: () => api.get('/vote/results'),
  resetVotes: (resetData) => api.post('/vote/reset', resetData),
};

export default api;
