import axios from 'axios';
const api=axios.create({baseURL:import.meta.env.VITE_API_URL||'http://localhost:8000/api'});
api.interceptors.request.use(c=>{const token=localStorage.getItem('access'); if(token)c.headers.Authorization=`Bearer ${token}`; return c;});
api.interceptors.response.use(r=>r,async error=>{const original=error.config;if(original&&error.response?.status===401&&!original._retry&&localStorage.getItem('refresh')){original._retry=true;try{const {data}=await axios.post(`${api.defaults.baseURL}/auth/token/refresh/`,{refresh:localStorage.getItem('refresh')});localStorage.setItem('access',data.access);if(data.refresh)localStorage.setItem('refresh',data.refresh);original.headers={...original.headers,Authorization:`Bearer ${data.access}`};return api(original);}catch{localStorage.removeItem('access');localStorage.removeItem('refresh');if(location.pathname!=='/login')location.assign('/login');}}return Promise.reject(error);});
export default api;
