/**
 * ТИПЫ ДЛЯ СОЗДАНИЯ ПОСТАВОК ПОСТАВЩИКОВ
 *
 * Выделены из create-suppliers-supply-page.tsx
 * Согласно rules-complete.md 9.7
 */

// Основные сущности
export interface GoodsSupplier {
  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
  rating?: number
  market?: string // Принадлежность к рынку согласно rules-complete.md v10.0
}

export interface GoodsProduct {
  id: string
  name: string
  description?: string
  price: number
  category?: { name: string }
  images: string[]
  mainImage?: string
  article: string // Артикул поставщика
  organization: {
    id: string
    name: string
  }
  quantity?: number
  unit?: string
  weight?: number
  dimensions?: {
    length: number
    width: number
    height: number
  }
}

export interface SelectedGoodsItem {
  id: string
  name: string
  sku: string
  price: number
  selectedQuantity: number
  unit?: string
  category?: string
  supplierId: string
  supplierName: string
  completeness?: string // Комплектность согласно rules2.md 9.7.2
  recipe?: string // Рецептура/состав
  specialRequirements?: string // Особые требования
  parameters?: Array<{ name: string; value: string }> // Параметры товара
}

// Компоненты рецептуры
export interface FulfillmentService {
  id: string
  name: string
  description?: string
  price: number
  category?: string
}

export interface FulfillmentConsumable {
  id: string
  name: string
  price: number
  quantity: number
  unit?: string
}

export interface SellerConsumable {
  id: string
  name: string
  pricePerUnit: number
  warehouseStock: number
  unit?: string
}

export interface WBCard {
  id: string
  title: string
  nmID: string
  vendorCode?: string
  brand?: string
}

export interface ProductRecipe {
  productId: string
  selectedServices: string[]
  selectedFFConsumables: string[]
  selectedSellerConsumables: string[]
  selectedWBCard?: string
}

// Состояния компонента
export interface SupplyCreationState {
  selectedSupplier: GoodsSupplier | null
  selectedGoods: SelectedGoodsItem[]
  searchQuery: string
  productSearchQuery: string
  deliveryDate: string
  selectedLogistics: string
  selectedFulfillment: string
  allSelectedProducts: Array<GoodsProduct & { selectedQuantity: number }>
  productRecipes: Record<string, ProductRecipe>
  productQuantities: Record<string, number>
}

// Действия для управления состоянием
export interface SupplyCreationActions {
  setSelectedSupplier: (supplier: GoodsSupplier | null) => void
  setSelectedGoods: (goods: SelectedGoodsItem[] | ((prev: SelectedGoodsItem[]) => SelectedGoodsItem[])) => void
  setSearchQuery: (query: string) => void
  setDeliveryDate: (date: string) => void
  setSelectedLogistics: (logistics: string) => void
  setSelectedFulfillment: (fulfillment: string) => void
  setAllSelectedProducts: (
    products:
      | Array<GoodsProduct & { selectedQuantity: number }>
      | ((
          prev: Array<GoodsProduct & { selectedQuantity: number }>,
        ) => Array<GoodsProduct & { selectedQuantity: number }>),
  ) => void
  setProductRecipes: (
    recipes: Record<string, ProductRecipe> | ((prev: Record<string, ProductRecipe>) => Record<string, ProductRecipe>),
  ) => void
  setProductQuantities: (
    quantities: Record<string, number> | ((prev: Record<string, number>) => Record<string, number>),
  ) => void
}

// Пропсы для блок-компонентов
export interface SuppliersBlockProps {
  suppliers: GoodsSupplier[]
  selectedSupplier: GoodsSupplier | null
  searchQuery: string
  loading: boolean
  onSupplierSelect: (supplier: GoodsSupplier) => void
  onSearchChange: (query: string) => void
}

export interface ProductCardsBlockProps {
  products: GoodsProduct[]
  selectedSupplier: GoodsSupplier | null
  onProductAdd: (product: GoodsProduct) => void
}

export interface DetailedCatalogBlockProps {
  allSelectedProducts: Array<GoodsProduct & { selectedQuantity: number }>
  productRecipes: Record<string, ProductRecipe>
  fulfillmentServices: FulfillmentService[]
  fulfillmentConsumables: FulfillmentConsumable[]
  sellerConsumables: SellerConsumable[]
  deliveryDate: string
  selectedFulfillment: string
  allCounterparties: GoodsSupplier[]
  onQuantityChange: (productId: string, quantity: number) => void
  onRecipeChange: (productId: string, recipe: ProductRecipe) => void
  onDeliveryDateChange: (date: string) => void
  onFulfillmentChange: (fulfillment: string) => void
  onProductRemove: (productId: string) => void
}

export interface CartBlockProps {
  selectedGoods: SelectedGoodsItem[]
  selectedSupplier: GoodsSupplier | null
  deliveryDate: string
  selectedFulfillment: string
  selectedLogistics: string
  allCounterparties: GoodsSupplier[]
  totalAmount: number
  isFormValid: boolean
  isCreatingSupply: boolean
  onLogisticsChange: (logistics: string) => void
  onCreateSupply: () => void
  onItemRemove: (itemId: string) => void
}

// Утилиты для расчетов
export interface RecipeCostCalculation {
  services: number
  consumables: number
  total: number
}

export interface SupplyCreationFormData {
  supplierId: string
  fulfillmentCenterId: string
  items: Array<{
    productId: string
    quantity: number
    recipe: ProductRecipe
  }>
  deliveryDate: string
  logistics: string
  specialRequirements?: string
}
