import { api } from '../api'

export interface Product {
  id: string
  sku: string
  name: string
  nameAr?: string
  description?: string
  categoryId: string
  category?: any
  brand?: string
  model?: string
  specifications?: any
  trackBy: string
  costPrice: number
  sellingPrice: number
  minStock: number
  maxStock?: number
  totalStock?: number
  inventoryItems?: any[]
  createdAt: string
  updatedAt: string
}

export interface CreateProductData {
  sku: string
  name: string
  nameAr?: string
  description?: string
  categoryId: string
  brand?: string
  model?: string
  trackBy?: string
  costPrice: number
  sellingPrice: number
  minStock?: number
  maxStock?: number
}

export interface UpdateProductData extends Partial<CreateProductData> {}

export interface ProductFilters {
  search?: string
  categoryId?: string
  brand?: string
  page?: number
  pageSize?: number
}

export interface ProductsResponse {
  products: Product[]
  total: number
  page: number
  pageSize: number
  totalPages: number
}

export const productsAPI = {
  // Get all products with filters and pagination
  getAll: async (filters: ProductFilters = {}): Promise<ProductsResponse> => {
    const params = new URLSearchParams()
    if (filters.search) params.append('search', filters.search)
    if (filters.categoryId) params.append('categoryId', filters.categoryId)
    if (filters.brand) params.append('brand', filters.brand)
    if (filters.page) params.append('page', filters.page.toString())
    if (filters.pageSize) params.append('pageSize', filters.pageSize.toString())

    const response = await api.get(`/inventory/products?${params.toString()}`)
    const { data, pagination } = response.data
    return {
      products: data || [],
      total: pagination?.total || 0,
      page: pagination?.page || 1,
      pageSize: pagination?.pageSize || 20,
      totalPages: pagination?.totalPages || 0,
    }
  },

  // Get single product by ID
  getById: async (id: string): Promise<Product> => {
    const response = await api.get(`/inventory/products/${id}`)
    return response.data.data
  },

  // Create new product
  create: async (data: CreateProductData): Promise<Product> => {
    const response = await api.post('/inventory/products', data)
    return response.data.data
  },

  // Update existing product
  update: async (id: string, data: UpdateProductData): Promise<Product> => {
    const response = await api.put(`/inventory/products/${id}`, data)
    return response.data.data
  },

  // Delete product
  delete: async (id: string): Promise<void> => {
    await api.delete(`/inventory/products/${id}`)
  },

  // Adjust stock
  adjustStock: async (
    productId: string,
    warehouseId: string,
    quantity: number,
    type: 'ADD' | 'REMOVE'
  ): Promise<any> => {
    const response = await api.post(`/inventory/products/${productId}/adjust-stock`, {
      warehouseId,
      quantity,
      type
    })
    return response.data.data
  },

  // Get product history
  getHistory: async (id: string): Promise<any[]> => {
    const response = await api.get(`/inventory/products/${id}/history`)
    return response.data.data
  }
}

// Categories API
export const categoriesAPI = {
  getAll: async (): Promise<any[]> => {
    const response = await api.get('/inventory/categories')
    return response.data.data
  }
}
