import axios from 'axios';
import { API_BASE_URL } from '../utils/constants';

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

handlerApi.interceptors.request.use((config) => {
  const token = localStorage.getItem('handlerToken');
  if (token) config.headers.Authorization = `Bearer ${token}`;
  return config;
});

export const handlerLogin = async (email, password) => {
  const response = await handlerApi.post('/login', { email, password });
  return response.data;
};

export const setHandlerPassword = async (email, newPassword) => {
  const response = await handlerApi.post('/set-password', { email, newPassword });
  return response.data;
};

export const getHandlerMe = async () => {
  const response = await handlerApi.get('/me');
  return response.data;
};

export const updateHandlerMe = async (data) => {
  const response = await handlerApi.put('/me', data);
  return response.data;
};
