import axios from "axios"
import { useAuthStore } from "@/stores/auth";

const api = axios.create({
    baseURL: process.env.VUE_APP_BASE_URL,
    clientId : process.env.VUE_APP_CLIENT_ID,
    clientSecret : process.env.VUE_APP_CLIENT_SECRET,
    headers:{
        "Content-Type":"application/json"
    }
})

api.interceptors.request.use(
    config =>{
        const token = localStorage.getItem('token')
        console.log(token)
        if(token){
            config.headers.Authorization = `Bearer ${token}`
        }
        return config
    }
)

api.interceptors.response.use(
    response => response,
    async error => {
        const originalRequest = error.config;
        const auth = useAuthStore();

        if(error.response && error.response.status === 401 && !originalRequest._retry){
            originalRequest._retry =true;
            try {
                const res = await axios.post(`${this.baseURL}` + "/oauth/token/refresh/", {},{withCredentials:true});
                
                auth.token = res.data.access_token;
                originalRequest.headers.Authorization = `Bearer ${auth.token}`;
                // error.config.headers.Authorization = `Bearer ${res.data.access}`;
                // return api.request(error.config);
                return api(originalRequest)
            } catch (refreshError) {
                // jika refresh juga gagal, logout
                window.location = "/login";
                auth.logout();
                return Promise.reject(refreshError)
            }
        }
        return Promise.reject(error)
    }
)

export default api