/**
 * Authentication API
 */

import axios from 'axios';
import { API_BASE_URL } from '../config/api';
import type { LoginCredentials, AuthResponse, User } from '../types';
import apiClient from './client';

// Login uses the main auth endpoint, not the mobile API
const authAxios = axios.create({
  baseURL: API_BASE_URL,
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json',
  },
});

export async function loginApi(credentials: LoginCredentials): Promise<AuthResponse> {
  const response = await authAxios.post<AuthResponse>('/auth/login/', credentials);
  return response.data;
}

export async function getProfile(): Promise<User> {
  const response = await apiClient.get<User>('/me/');
  return response.data;
}

export async function logoutApi(): Promise<void> {
  try {
    await apiClient.post('/logout/');
  } catch (error) {
    // Ignore logout errors
  }
}
