import apiClient from '../../../shared/services/apiClient';

export interface InventoryItem {
  id: number;
  name: string;
  description?: string;
  category: string;
  unit: string;
  current_quantity: number;
  minimum_quantity: number;
  maximum_quantity?: number;
  reorder_quantity?: number;
  unit_cost?: number;
  supplier?: string;
  storage_location?: string;
  is_active: boolean;
  is_tracked: boolean;
  barcode?: string;
  sku?: string;
  is_low_stock: boolean;
  created_at?: string;
}

export interface InventoryTransaction {
  id: number;
  transaction_type: string;
  quantity: number;
  quantity_before: number;
  quantity_after: number;
  notes?: string;
  cost?: number;
  transaction_date: string;
}

export interface ReorderRequest {
  id: number;
  item_id: number;
  item_name: string;
  requested_quantity: number;
  current_quantity: number;
  minimum_quantity: number;
  status: string;
  priority: string;
  notes?: string;
  requested_at: string;
}

const inventoryService = {
  async getInventoryItems(params?: {
    category?: string;
    low_stock?: boolean;
    is_active?: boolean;
    page?: number;
    limit?: number;
  }) {
    const response = await apiClient.get('/inventory/items', { params });
    return response.data;
  },

  async getInventoryItem(itemId: number) {
    const response = await apiClient.get(`/inventory/items/${itemId}`);
    return response.data;
  },

  async createInventoryItem(data: {
    name: string;
    description?: string;
    category: string;
    unit: string;
    minimum_quantity: number;
    maximum_quantity?: number;
    reorder_quantity?: number;
    unit_cost?: number;
    supplier?: string;
    supplier_contact?: string;
    storage_location?: string;
    barcode?: string;
    sku?: string;
    notes?: string;
  }) {
    const response = await apiClient.post('/inventory/items', data);
    return response.data;
  },

  async updateInventoryItem(itemId: number, data: Partial<InventoryItem>) {
    const response = await apiClient.put(`/inventory/items/${itemId}`, data);
    return response.data;
  },

  async createTransaction(data: {
    item_id: number;
    transaction_type: string;
    quantity: number;
    notes?: string;
    cost?: number;
    reference_type?: string;
    reference_id?: number;
  }) {
    const response = await apiClient.post('/inventory/transactions', data);
    return response.data;
  },

  async getItemTransactions(itemId: number, params?: { page?: number; limit?: number }) {
    const response = await apiClient.get(`/inventory/items/${itemId}/transactions`, { params });
    return response.data;
  },

  async createReorderRequest(data: {
    item_id: number;
    requested_quantity: number;
    priority?: string;
    notes?: string;
  }) {
    const response = await apiClient.post('/inventory/reorder-requests', data);
    return response.data;
  },

  async getReorderRequests(params?: { status?: string; page?: number; limit?: number }) {
    const response = await apiClient.get('/inventory/reorder-requests', { params });
    return response.data;
  },

  async recordTaskConsumption(data: {
    task_id: number;
    item_id: number;
    quantity: number;
    notes?: string;
  }) {
    const response = await apiClient.post('/inventory/task-consumption', data);
    return response.data;
  },

  async getLowStockItems() {
    const response = await apiClient.get('/inventory/low-stock');
    return response.data;
  },
};

export default inventoryService;

