import { api } from '@/services/api'

export interface Category {
  id: number
  name: string
  slug: string
  status: string
}

export interface CategoryPayload {
  name?: string
  status?: string
}

export interface Product {
  id: number
  category?: {
    id: number
    name: string
  } | null
  name: string
  sku: string | null
  barcode: string | null
  purchase_price: string
  selling_price: string
  current_stock: string
  low_stock_threshold: string
  is_low_stock: boolean
  unit: string
  status: string
}

export interface ProductFilters {
  search?: string
  status?: string
  category_id?: number
  low_stock?: boolean
  page?: number
  per_page?: number
}

export interface ProductPayload {
  category_id?: number | null
  name: string
  sku?: string | null
  barcode?: string | null
  purchase_price?: number
  selling_price?: number
  low_stock_threshold?: number
  unit?: string
  status?: string
}

export interface Supplier {
  id: number
  name: string
  email: string | null
  phone: string | null
  address: string | null
  status: string
}

export interface SupplierFilters {
  search?: string
  status?: string
}

export interface SupplierPayload {
  name: string
  email?: string | null
  phone?: string | null
  address?: string | null
  status?: string
}

export interface PurchaseItem {
  id: number
  product: {
    id: number
    name: string
    sku: string | null
  }
  quantity: string
  unit_cost: string
  line_total: string
}

export interface Purchase {
  id: number
  purchase_number: string
  supplier?: {
    id: number
    name: string
  } | null
  financial_account?: {
    id: number
    name: string
    type: string
  } | null
  purchase_date: string
  subtotal: string
  total: string
  status: string
  notes?: string | null
  items?: PurchaseItem[]
}

export interface PurchaseFilters {
  search?: string
  supplier_id?: number
  status?: string
  date_from?: string
  date_to?: string
  page?: number
  per_page?: number
}

export interface PaginationMeta {
  current_page: number
  from: number | null
  last_page: number
  per_page: number
  to: number | null
  total: number
}

export interface Paginated<T> {
  data: T[]
  meta: PaginationMeta
}

export const inventoryService = {
  async categories(status = 'active') {
    const { data } = await api.get<{ data: Category[] }>('/inventory/categories', { params: { status } })

    return data.data
  },

  async createCategory(payload: { name: string }) {
    const { data } = await api.post<{ data: Category }>('/inventory/categories', payload)

    return data.data
  },

  async updateCategory(category: Category, payload: CategoryPayload) {
    const { data } = await api.patch<{ data: Category }>(`/inventory/categories/${category.id}`, payload)

    return data.data
  },

  async products(filters: ProductFilters = {}) {
    const { data } = await api.get<{ data: Product[] }>('/inventory/products', { params: filters })

    return data.data
  },

  async paginatedProducts(filters: ProductFilters = {}) {
    const { data } = await api.get<{ data: Paginated<Product> }>('/inventory/products', { params: filters })

    return data.data
  },

  async exportProductsCsv(filters: ProductFilters = {}) {
    const { data } = await api.get<Blob>('/inventory/products/export/csv', {
      params: filters,
      responseType: 'blob',
    })

    return data
  },

  async lowStockProducts() {
    const { data } = await api.get<{ data: Product[] }>('/inventory/products/low-stock')

    return data.data
  },

  async lookupProduct(code: string) {
    const { data } = await api.get<{ data: Product }>('/inventory/products/lookup', {
      params: { code },
    })

    return data.data
  },

  async createProduct(payload: ProductPayload) {
    const { data } = await api.post<{ data: Product }>('/inventory/products', payload)

    return data.data
  },

  async updateProduct(product: Product, payload: ProductPayload) {
    const { data } = await api.patch<{ data: Product }>(`/inventory/products/${product.id}`, payload)

    return data.data
  },

  async adjustStock(product: Product, quantity: number) {
    const { data } = await api.post<{ data: Product }>(`/inventory/products/${product.id}/adjust-stock`, {
      quantity,
      note: 'Manual adjustment',
    })

    return data.data
  },

  async suppliers(filters: SupplierFilters = {}) {
    const { data } = await api.get<{ data: Supplier[] }>('/inventory/suppliers', { params: filters })

    return data.data
  },

  async createSupplier(payload: SupplierPayload) {
    const { data } = await api.post<{ data: Supplier }>('/inventory/suppliers', payload)

    return data.data
  },

  async updateSupplier(supplier: Supplier, payload: SupplierPayload) {
    const { data } = await api.patch<{ data: Supplier }>(`/inventory/suppliers/${supplier.id}`, payload)

    return data.data
  },

  async purchases(filters: PurchaseFilters = {}) {
    const { data } = await api.get<{ data: Purchase[] }>('/inventory/purchases', { params: filters })

    return data.data
  },

  async paginatedPurchases(filters: PurchaseFilters = {}) {
    const { data } = await api.get<{ data: Paginated<Purchase> }>('/inventory/purchases', { params: filters })

    return data.data
  },

  async exportPurchasesCsv(filters: PurchaseFilters = {}) {
    const { data } = await api.get<Blob>('/inventory/purchases/export/csv', {
      params: filters,
      responseType: 'blob',
    })

    return data
  },

  async purchase(purchase: Purchase) {
    const { data } = await api.get<{ data: Purchase }>(`/inventory/purchases/${purchase.id}`)

    return data.data
  },

  async updatePurchase(purchase: Purchase, payload: { status?: 'cancelled'; notes?: string | null }) {
    const { data } = await api.patch<{ data: Purchase }>(`/inventory/purchases/${purchase.id}`, payload)

    return data.data
  },

  async receivePurchase(payload: {
    supplier_id?: number
    financial_account_id?: number
    purchase_date: string
    notes?: string
    items: Array<{
      product_id: number
      quantity: number
      unit_cost: number
    }>
  }) {
    const { data } = await api.post<{ data: Purchase }>('/inventory/purchases', payload)

    return data.data
  },
}
