import axios from 'axios';

const api = axios.create({
    baseURL: import.meta.env.VITE_API_URL || 'http://localhost:8000/api',
    timeout: 8000, // 增加超时时间到 10秒
    headers: {
        'Content-Type': 'application/json',
    }
});

// 请求拦截器：自动附加 Token
api.interceptors.request.use((config) => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
}, (error) => {
    return Promise.reject(error);
});

export const getConfigs = () => api.get('/configs/');
export const createOrder = (data) => api.post('/orders/', data);
export const nativePay = (data) => api.post('/pay/', data);
export const getOrder = (id) => api.get(`/orders/${id}/`);
export const queryOrderStatus = (id) => api.get(`/orders/${id}/query_status/`);
export const initiatePayment = (orderId) => api.post(`/orders/${orderId}/initiate_payment/`);
export const confirmPayment = (orderId) => api.post(`/orders/${orderId}/confirm_payment/`);

export const getServices = () => api.get('/services/');
export const getServiceDetail = (id) => api.get(`/services/${id}/`);
export const createServiceOrder = (data) => api.post('/service-orders/', data);
export const getVCCourses = () => api.get('/courses/');
export const getVCCourseDetail = (id) => api.get(`/courses/${id}/`);
export const enrollCourse = (data) => api.post('/course-enrollments/', data);

export const sendSms = (data) => api.post('/auth/send-sms/', data);
export const queryMyOrders = (data) => api.post('/orders/my_orders/', data);
export const phoneLogin = (data) => api.post('/auth/phone-login/', data);
export const getUserInfo = () => api.get('/users/me/');
export const updateUserInfo = (data) => api.post('/wechat/update/', data);
export const uploadUserAvatar = (data) => {
    // 使用 axios 直接请求外部接口，避免 base URL 和拦截器干扰
    return axios.post('https://data.tangledup-ai.com/upload?folder=uploads/market/avator', data, {
        headers: {
            'Content-Type': 'multipart/form-data',
        }
    });
};

// Community / Forum API
export const getTopics = (params) => api.get('/community/topics/', { params });
export const getTopicDetail = (id) => api.get(`/community/topics/${id}/`);
export const createTopic = (data) => api.post('/community/topics/', data);
export const updateTopic = (id, data) => api.patch(`/community/topics/${id}/`, data);
export const getReplies = (params) => api.get('/community/replies/', { params });
export const createReply = (data) => api.post('/community/replies/', data);
export const uploadMedia = (data) => {
    return api.post('/community/media/', data, {
        headers: {
            'Content-Type': 'multipart/form-data',
        }
    });
};
export const getStarUsers = () => api.get('/users/stars/');
export const getMyPaidItems = () => api.get('/users/paid-items/');
export const getAnnouncements = () => api.get('/community/announcements/');
export const getActivities = () => api.get('/community/activities/');
export const getActivityDetail = (id) => api.get(`/community/activities/${id}/`);
export const signUpActivity = (id, data) => api.post(`/community/activities/${id}/signup/`, data);
export const getMySignups = () => api.get('/community/activities/my_signups/');

export default api;
