import axios from "axios";
import { useAuthStore } from "@/store/authStore";

const apiClient = axios.create({
  baseURL: "http://192.168.149.101:8000/api",
  headers: { "Content-Type": "application/json" },
});

apiClient.interceptors.request.use((config) => {
  const token = useAuthStore.getState().token;
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

/**
 * POST /api/seats/{seat_id}/lock?user_id={user_id}
 * Бросает AxiosError со status 409, если место захвачено конкурентом.
 */
export async function lockSeatApi(seatId: number, userId: number): Promise<void> {
  await apiClient.post(`/seats/${seatId}/lock`, null, {
    params: { user_id: userId },
  });
}

export default apiClient;
