import axios from "axios"

export const API_CONFIG = {
  baseURL: (import.meta.env.VITE_API_URL ? import.meta.env.VITE_API_URL : '') + "/api",
  wsURL: "http://localhost:3000",
  timeout: 10000,
  retryAttempts: 3,
}

export const apiClient = axios.create({
  baseURL: API_CONFIG.baseURL,
  timeout: API_CONFIG.timeout,
  withCredentials: true, // Important for cookie-based auth
  headers: {
    'Content-Type': 'application/json'
  }
});

//login
export const login = async (username, password) => {
  try {
    const response = await apiClient.post("/users/login", { username, password })
    return response
  } catch (error) {
    throw error
  }
}

//register
export const register = async (username, email, password, fname, lname, phone) => {
  try {
    const response = await apiClient.post("/users/create", { username, email, password, fname, lname, phone })
    return response
  } catch (error) {
    throw error
  }
}

//verify email
export const verifyEmail = async (token) => {
  try {
    const response = await apiClient.get(`/users/verify-email/${token}`)
    return response.data
  } catch (error) {
    throw error
  }
}

// Get current user's game statistics
export const getUserStats = async () => {
  try {
    const response = await apiClient.get("/users/me/stats")
    return response.data
  } catch (error) {
    throw error
  }
}
