// =============================================================================
// 🔧 ТИПЫ ДЛЯ СИСТЕМЫ СОЗДАНИЯ ПОСТАВОК РАСХОДНИКОВ ФУЛФИЛМЕНТА V2
// =============================================================================
// Эти типы экстрагированы из монолитного компонента в соответствии с правилами
// модульной архитектуры MODULAR_ARCHITECTURE_PATTERN.md

// 🏢 ТИПЫ ПОСТАВЩИКОВ И ОРГАНИЗАЦИЙ
export interface FulfillmentConsumableSupplier {
  id: string
  inn: string
  name?: string
  fullName?: string
  type: 'FULFILLMENT' | 'SELLER' | 'LOGIST' | 'WHOLESALE'
  address?: string
  phones?: Array<{ value: string }>
  emails?: Array<{ value: string }>
  users?: Array<{ id: string; avatar?: string; managerName?: string }>
  createdAt: string
}

// 📦 ТИПЫ ТОВАРОВ И РАСХОДНИКОВ
export interface FulfillmentConsumableProduct {
  id: string
  name: string
  description?: string
  price: number
  type?: 'PRODUCT' | 'CONSUMABLE'
  category?: { name: string }
  images: string[]
  mainImage?: string
  organization: {
    id: string
    name: string
  }
  stock?: number
  unit?: string
  quantity?: number
  ordered?: number
}

// 🛒 ТИПЫ КОРЗИНЫ И ВЫБРАННЫХ ТОВАРОВ
export interface SelectedFulfillmentConsumable {
  id: string
  name: string
  price: number
  selectedQuantity: number
  unit?: string
  category?: string
  supplierId: string
  supplierName: string
}

// 📋 ТИПЫ ФОРМЫ СОЗДАНИЯ ПОСТАВКИ
export interface CreateSupplyFormData {
  selectedSupplier: FulfillmentConsumableSupplier | null
  selectedLogistics: FulfillmentConsumableSupplier | null
  selectedConsumables: SelectedFulfillmentConsumable[]
  searchQuery: string
  productSearchQuery: string
  deliveryDate: string
  notes: string
  isCreatingSupply: boolean
}

// 🔍 ТИПЫ ПОИСКОВЫХ ПАРАМЕТРОВ
export interface SupplierSearchParams {
  searchQuery: string
  selectedSupplier: FulfillmentConsumableSupplier | null
}

export interface ProductSearchParams {
  productSearchQuery: string
  selectedSupplier: FulfillmentConsumableSupplier | null
  type: 'CONSUMABLE'
}

// 📊 ТИПЫ ДАННЫХ ЗАПРОСОВ
export interface CounterpartiesData {
  myCounterparties: FulfillmentConsumableSupplier[]
}

export interface ProductsData {
  organizationProducts: FulfillmentConsumableProduct[]
}

// 🎯 ТИПЫ ДЕЙСТВИЙ И ОПЕРАЦИЙ
export interface QuantityUpdateParams {
  productId: string
  quantity: number
  product: FulfillmentConsumableProduct
  selectedSupplier: FulfillmentConsumableSupplier
}

export interface SupplyCreationInput {
  supplierId: string
  logisticsPartnerId?: string  // Добавляем логистического партнера
  requestedDeliveryDate: string
  items: Array<{
    productId: string
    requestedQuantity: number
  }>
  notes?: string
}

// 🔧 ТИПЫ УТИЛИТ И ХЕЛПЕРОВ
export interface StockCalculation {
  totalStock: number
  orderedStock: number
  availableStock: number
}

export interface CurrencyFormatOptions {
  style: 'currency'
  currency: 'RUB'
  minimumFractionDigits: 0
}

// 🚦 ТИПЫ СОСТОЯНИЙ ЗАГРУЗКИ
export interface LoadingStates {
  counterpartiesLoading: boolean
  productsLoading: boolean
  isCreatingSupply: boolean
}

// ⚠️ ТИПЫ ОШИБОК И ВАЛИДАЦИИ
export interface ValidationError {
  field: string
  message: string
}

export interface StockValidationResult {
  isValid: boolean
  availableStock: number
  requestedQuantity: number
  error?: string
}

// 🎨 ТИПЫ UI КОМПОНЕНТОВ
export interface SupplierCardProps {
  supplier: FulfillmentConsumableSupplier
  isSelected: boolean
  onSelect: (supplier: FulfillmentConsumableSupplier) => void
  index: number
}

export interface ProductCardProps {
  product: FulfillmentConsumableProduct
  selectedQuantity: number
  onUpdateQuantity: (productId: string, quantity: number) => void
  index: number
}

export interface ShoppingCartProps {
  selectedConsumables: SelectedFulfillmentConsumable[]
  deliveryDate: string
  notes: string
  selectedLogistics: FulfillmentConsumableSupplier | null
  logisticsPartners: FulfillmentConsumableSupplier[]
  isCreatingSupply: boolean
  onUpdateQuantity: (productId: string, quantity: number) => void
  onSetDeliveryDate: (date: string) => void
  onSetNotes: (notes: string) => void
  onSetLogistics: (logistics: FulfillmentConsumableSupplier | null) => void
  onCreateSupply: () => void
}

// 📈 ТИПЫ ВЫЧИСЛЕНИЙ И АНАЛИТИКИ
export interface SupplyTotals {
  totalAmount: number
  totalItems: number
  itemsCount: number
}

// 🎯 ТИПЫ СОБЫТИЙ И КОЛБЭКОВ
export type SupplierSelectHandler = (supplier: FulfillmentConsumableSupplier) => void
export type QuantityUpdateHandler = (productId: string, quantity: number) => void
export type SupplyCreateHandler = () => Promise<void>
export type SearchUpdateHandler = (query: string) => void

// 💾 ТИПЫ СТОРЕЙДЖА И КЕША
export interface CachedSupplierData {
  suppliers: FulfillmentConsumableSupplier[]
  lastFetch: string
}

export interface CachedProductData {
  products: FulfillmentConsumableProduct[]
  supplierId: string
  lastFetch: string
}

// 🌐 ТИПЫ ГРАФК ЗАПРОСОВ
export interface GraphQLQueryVariables {
  organizationId?: string
  search?: string | null
  category?: string | null
  type?: 'CONSUMABLE'
}

export interface CreateSupplyMutationResponse {
  createFulfillmentConsumableSupply: {
    success: boolean
    message?: string
  }
}

// 🔄 ЭКСПОРТ ТИПОВ ДЛЯ ХУКОВ
export interface UseSupplierDataReturn {
  suppliers: FulfillmentConsumableSupplier[]
  logisticsPartners: FulfillmentConsumableSupplier[]
  filteredSuppliers: FulfillmentConsumableSupplier[]
  loading: boolean
  error: Error | null
}

export interface UseProductDataReturn {
  products: FulfillmentConsumableProduct[]
  loading: boolean
  error: Error | null
  refetch: () => void
}

export interface UseSupplyFormReturn {
  selectedSupplier: FulfillmentConsumableSupplier | null
  selectedLogistics: FulfillmentConsumableSupplier | null
  selectedConsumables: SelectedFulfillmentConsumable[]
  searchQuery: string
  productSearchQuery: string
  deliveryDate: string
  notes: string
  setSelectedSupplier: (supplier: FulfillmentConsumableSupplier | null) => void
  setSelectedLogistics: (logistics: FulfillmentConsumableSupplier | null) => void
  setSelectedConsumables: (consumables: SelectedFulfillmentConsumable[]) => void
  setSearchQuery: (query: string) => void
  setProductSearchQuery: (query: string) => void
  setDeliveryDate: (date: string) => void
  setNotes: (notes: string) => void
  resetForm: () => void
}

export interface UseQuantityManagementReturn {
  updateConsumableQuantity: (productId: string, quantity: number) => void
  getSelectedQuantity: (productId: string) => number
  getTotalAmount: () => number
  getTotalItems: () => number
}

export interface UseSupplyCreationReturn {
  createSupply: () => Promise<void>
  isCreating: boolean
  error: string | null
}