import { API_BASE_URL } from '../utils/constants';
import { apiClient } from './apiClient';

interface AuthResponse {
  access_token: string;
}

export const authService = {
  async login(username: string, password: string): Promise<AuthResponse> {
    const language = localStorage.getItem('userLanguage') || 'ja';
    const response = await fetch(`${API_BASE_URL}/auth/login`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-user-language': language,
      },
      body: JSON.stringify({ username, password }),
    });

    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || 'Login failed');
    }

    return response.json();
  },



  async getProfile(token: string): Promise<any> {
    const response = await apiClient.request('/auth/profile', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });

    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || 'Failed to fetch profile');
    }

    return response.json();
  },
};
