/**
 * API Client
 * Generated from workflow models
 */

const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000/api';

class ApiClient {
  private async request(endpoint: string, options?: RequestInit) {
    const token = localStorage.getItem('auth_token');
    const response = await fetch(`${API_BASE_URL}${endpoint}`, {
      ...options,
      headers: {
        'Content-Type': 'application/json',
        ...(token && { Authorization: `Bearer ${token}` }),
        ...options?.headers,
      },
    });

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

    return response.json();
  }

  User = {
    list: () => this.request('/Users'),
    get: (id: string) => this.request(`/Users/${id}`),
    create: (data: any) => this.request('/Users', {
      method: 'POST',
      body: JSON.stringify(data),
    }),
    update: (id: string, data: any) => this.request(`/Users/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),
    delete: (id: string) => this.request(`/Users/${id}`, {
      method: 'DELETE',
    }),
  };

  Task = {
    list: () => this.request('/Tasks'),
    get: (id: string) => this.request(`/Tasks/${id}`),
    create: (data: any) => this.request('/Tasks', {
      method: 'POST',
      body: JSON.stringify(data),
    }),
    update: (id: string, data: any) => this.request(`/Tasks/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),
    delete: (id: string) => this.request(`/Tasks/${id}`, {
      method: 'DELETE',
    }),
  };

}

export const api = new ApiClient();
