// Linux BenchTools - API Client

const API_BASE_URL = (window.BenchConfig && window.BenchConfig.backendApiUrl)
  || `${window.location.protocol}//${window.location.hostname}:8007/api`;

class BenchAPI {
  constructor(baseURL = API_BASE_URL) {
    this.baseURL = baseURL;
  }

  // Generic request handler
  async request(endpoint, options = {}) {
    const url = `${this.baseURL}${endpoint}`;

    try {
      const response = await fetch(url, {
        headers: {
          'Content-Type': 'application/json',
          ...options.headers
        },
        ...options
      });

      if (!response.ok) {
        const errorData = await response.json().catch(() => ({}));
        const errorMessage = errorData.detail || errorData.message || `HTTP ${response.status}: ${response.statusText}`;

        // Create a detailed error object
        const apiError = new Error(errorMessage);
        apiError.status = response.status;
        apiError.statusText = response.statusText;
        apiError.endpoint = endpoint;
        apiError.response = errorData;

        throw apiError;
      }

      // Handle 204 No Content
      if (response.status === 204) {
        return null;
      }

      const contentType = response.headers.get('content-type');
      if (contentType && contentType.includes('application/json')) {
        return await response.json();
      }

      // Return text for non-JSON responses
      return await response.text();
    } catch (error) {
      console.error(`API Error [${endpoint}]:`, error);

      // Handle network errors
      if (error.name === 'TypeError' && error.message === 'Failed to fetch') {
        error.message = 'Impossible de se connecter au serveur backend. Vérifiez que le service est démarré.';
      }

      throw error;
    }
  }

  // GET request
  async get(endpoint, params = {}) {
    const queryString = new URLSearchParams(params).toString();
    const url = queryString ? `${endpoint}?${queryString}` : endpoint;
    return this.request(url, { method: 'GET' });
  }

  // POST request
  async post(endpoint, data) {
    return this.request(endpoint, {
      method: 'POST',
      body: JSON.stringify(data)
    });
  }

  // PUT request
  async put(endpoint, data) {
    return this.request(endpoint, {
      method: 'PUT',
      body: JSON.stringify(data)
    });
  }

  // DELETE request
  async delete(endpoint) {
    return this.request(endpoint, { method: 'DELETE' });
  }

  // Upload file
  async upload(endpoint, formData) {
    const url = `${this.baseURL}${endpoint}`;

    try {
      const response = await fetch(url, {
        method: 'POST',
        body: formData
        // Don't set Content-Type header, let browser set it with boundary
      });

      if (!response.ok) {
        const errorData = await response.json().catch(() => ({}));
        throw new Error(errorData.detail || `HTTP ${response.status}: ${response.statusText}`);
      }

      return await response.json();
    } catch (error) {
      console.error(`Upload Error [${endpoint}]:`, error);
      throw error;
    }
  }

  // ==================== Devices ====================

  // Get all devices
  async getDevices(params = {}) {
    return this.get('/devices', params);
  }

  // Get device by ID
  async getDevice(deviceId) {
    return this.get(`/devices/${deviceId}`);
  }

  // Update device
  async updateDevice(deviceId, data) {
    return this.put(`/devices/${deviceId}`, data);
  }

  // Delete device
  async deleteDevice(deviceId) {
    return this.delete(`/devices/${deviceId}`);
  }

  // ==================== Benchmarks ====================

  // Get benchmarks for a device
  async getDeviceBenchmarks(deviceId, params = {}) {
    return this.get(`/devices/${deviceId}/benchmarks`, params);
  }

  // Get benchmark by ID
  async getBenchmark(benchmarkId) {
    return this.get(`/benchmarks/${benchmarkId}`);
  }

  // Update benchmark fields
  async updateBenchmark(benchmarkId, data) {
    return this.request(`/benchmarks/${benchmarkId}`, {
      method: 'PATCH',
      body: JSON.stringify(data)
    });
  }

  // Get all benchmarks
  async getAllBenchmarks(params = {}) {
    return this.get('/benchmarks', params);
  }

  // ==================== Links ====================

  // Get links for a device
  async getDeviceLinks(deviceId) {
    return this.get(`/devices/${deviceId}/links`);
  }

  // Add link to device
  async addDeviceLink(deviceId, data) {
    return this.post(`/devices/${deviceId}/links`, data);
  }

  // Update link
  async updateLink(linkId, data) {
    return this.put(`/links/${linkId}`, data);
  }

  // Delete link
  async deleteLink(linkId) {
    return this.delete(`/links/${linkId}`);
  }

  // ==================== Documents ====================

  // Get documents for a device
  async getDeviceDocs(deviceId) {
    return this.get(`/devices/${deviceId}/docs`);
  }

  // Upload document
  async uploadDocument(deviceId, file, docType) {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('doc_type', docType);

    return this.upload(`/devices/${deviceId}/docs`, formData);
  }

  // Delete document
  async deleteDocument(docId) {
    return this.delete(`/docs/${docId}`);
  }

  // Get document download URL
  getDocumentDownloadUrl(docId) {
    return `${this.baseURL}/docs/${docId}/download`;
  }

  // ==================== Health ====================

  // Health check
  async healthCheck() {
    return this.get('/health');
  }

  // ==================== Stats ====================

  // Get dashboard stats
  async getStats() {
    return this.get('/stats');
  }

  // ==================== Backup ====================

  // Create database backup
  async backupDatabase() {
    return this.request('/backup', { method: 'POST' });
  }
}

// Create global API instance
const api = new BenchAPI();

// Export for use in other files
window.BenchAPI = api;
