import axios from 'axios'

const API_BASE_URL = `${import.meta.env.VITE_API_BASE_URL || 'http://localhost:5352/api'}/device-warning`;

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

api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

api.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    console.error('API Error:', error.response ? error.response.data : error.message);
    return Promise.reject(error);
  }
);

export default {
  getDeviceWarnings: (params) => api.get('/', { params }),
  getDeviceWarningById: (id) => api.get(`/${id}`),
  createDeviceWarning: (data) => api.post('/', data),
  updateDeviceWarning: (id, data) => api.put(`/${id}`, data),
  deleteDeviceWarning: (id) => api.delete(`/${id}`),
  updateWarningStatus: (id, data) => api.patch(`/${id}/status`, data),
  getWarningStats: () => api.get('/stats'),
};
