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

interface ApiResponse<T = any> {
  data: T;
  status: number;
}

class ApiClient {
  private baseURL: string;

  constructor(baseURL: string) {
    this.baseURL = baseURL;
  }

  private getAuthHeaders(): Record<string, string> {
    // V2 API key auth (primary)
    const apiKey = localStorage.getItem('kb_api_key');
    const activeTenantId = localStorage.getItem('kb_active_tenant_id');
    // Legacy JWT token (fallback, kept for compatibility during transition)
    const token = localStorage.getItem('authToken') || localStorage.getItem('token');
    const language = localStorage.getItem('userLanguage') || 'ja';
    return {
      'Content-Type': 'application/json',
      'x-user-language': language,
      ...(apiKey && { 'x-api-key': apiKey }),
      ...(activeTenantId && { 'x-tenant-id': activeTenantId }),
      ...(token && { Authorization: `Bearer ${token}` }),
    };
  }

  // New API call method, returns { data, status }
  async get<T = any>(url: string): Promise<ApiResponse<T>> {
    const response = await fetch(`${this.baseURL}${url}`, {
      method: 'GET',
      headers: this.getAuthHeaders(),
    });

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

    const data = await response.json();
    return { data, status: response.status };
  }

  async post<T = any>(url: string, body?: any): Promise<ApiResponse<T>> {
    const response = await fetch(`${this.baseURL}${url}`, {
      method: 'POST',
      headers: this.getAuthHeaders(),
      body: body ? JSON.stringify(body) : undefined,
    });

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

    const data = await response.json();
    return { data, status: response.status };
  }

  async put<T = any>(url: string, body?: any): Promise<ApiResponse<T>> {
    const response = await fetch(`${this.baseURL}${url}`, {
      method: 'PUT',
      headers: this.getAuthHeaders(),
      body: body ? JSON.stringify(body) : undefined,
    });

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

    const data = await response.json();
    return { data, status: response.status };
  }

  async delete<T = any>(url: string): Promise<ApiResponse<T>> {
    const response = await fetch(`${this.baseURL}${url}`, {
      method: 'DELETE',
      headers: this.getAuthHeaders(),
    });

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

    const data = await response.json();
    return { data, status: response.status };
  }

  // Legacy compatibility method — returns raw Response for streaming and other special cases
  async request(path: string, options: RequestInit = {}): Promise<Response> {
    const apiKey = localStorage.getItem('kb_api_key');
    const activeTenantId = localStorage.getItem('kb_active_tenant_id');
    const token = localStorage.getItem('authToken');
    const headers = new Headers(options.headers);

    if (apiKey) headers.set('x-api-key', apiKey);
    if (activeTenantId) headers.set('x-tenant-id', activeTenantId);
    if (token) headers.set('Authorization', `Bearer ${token}`);

    const language = localStorage.getItem('userLanguage') || 'ja';
    headers.set('x-user-language', language);

    let url = path;
    if (!path.startsWith('http')) {
      const cleanPath = path.startsWith('/') ? path : `/${path}`;
      url = `${this.baseURL}${cleanPath}`;
    }

    const response = await fetch(url, {
      ...options,
      headers,
    });

    if (response.status === 401) {
      localStorage.removeItem('kb_api_key');
      localStorage.removeItem('authToken');
      window.location.href = '/login';
      throw new Error('Unauthorized');
    }

    return response;
  }
}

export const apiClient = new ApiClient(API_BASE_URL);