/**
 * ТИПЫ ДЛЯ ПРЯМОГО СОЗДАНИЯ ПОСТАВОК WB
 *
 * Выделены из direct-supply-creation.tsx
 * Согласно MODULAR_ARCHITECTURE_PATTERN.md
 */

// === ИМПОРТЫ ===
import { WildberriesCard } from '@/types/supplies'

// === ОСНОВНЫЕ СУЩНОСТИ ===

export interface SupplyItem {
  card: WildberriesCard
  quantity: number
  pricePerUnit: number
  totalPrice: number
  supplierId: string
  priceType: 'perUnit' | 'total' // за штуку или за общее количество
}

export interface Organization {
  id: string
  name?: string
  fullName?: string
  type: string
}

export interface FulfillmentService {
  id: string
  name: string
  description?: string
  price: number
  category?: string
}

export interface Supplier {
  id: string
  name: string
  contactName: string
  phone: string
  market: string
  address: string
  place: string
  telegram: string
}

export interface NewSupplier {
  name: string
  contactName: string
  phone: string
  market: string
  address: string
  place: string
  telegram: string
}

export interface SupplierErrors {
  name: string
  contactName: string
  phone: string
  telegram: string
}

// === СОСТОЯНИЯ КОМПОНЕНТА ===

export interface DirectSupplyState {
  // Новая система данных поставки
  deliveryDate: string
  selectedFulfillment: string
  goodsQuantity: number
  goodsVolume: number
  cargoPlaces: number
  goodsPrice: number
  fulfillmentServicesPrice: number
  logisticsPrice: number

  // Поиск и товары
  searchTerm: string
  loading: boolean
  wbCards: WildberriesCard[]
  supplyItems: SupplyItem[]

  // Оригинальные настройки
  deliveryDateOriginal: Date | undefined
  selectedFulfillmentOrg: string
  selectedServices: string[]
  selectedConsumables: string[]

  // Поставщики
  suppliers: Supplier[]
  showSupplierModal: boolean
  newSupplier: NewSupplier
  supplierErrors: SupplierErrors

  // Данные для фулфилмента
  organizationServices: { [orgId: string]: FulfillmentService[] }
  organizationSupplies: { [orgId: string]: FulfillmentService[] }
}

// === ПРОПСЫ КОМПОНЕНТА ===

export interface DirectSupplyCreationProps {
  onComplete: () => void
  onCreateSupply: () => void
  canCreateSupply: boolean
  isCreatingSupply: boolean
  onCanCreateSupplyChange?: (canCreate: boolean) => void
  selectedFulfillmentId?: string
  onServicesCostChange?: (cost: number) => void
  onItemsPriceChange?: (totalPrice: number) => void
  onItemsCountChange?: (hasItems: boolean) => void
  onConsumablesCostChange?: (cost: number) => void
  onVolumeChange?: (totalVolume: number) => void
  onSuppliersChange?: (suppliers: unknown[]) => void
}

// === ПРОПСЫ ДЛЯ БЛОК-КОМПОНЕНТОВ ===

export interface ProductSearchBlockProps {
  searchTerm: string
  loading: boolean
  onSearchChange: (term: string) => void
  onSearch: () => void
}

export interface ProductGridBlockProps {
  wbCards: WildberriesCard[]
  loading: boolean
  onAddToSupply: (card: WildberriesCard, quantity: number, supplierId: string) => void
}

export interface SupplyItemsBlockProps {
  supplyItems: SupplyItem[]
  onUpdateQuantity: (cardId: number, quantity: number) => void
  onUpdatePrice: (cardId: number, price: number, priceType: 'perUnit' | 'total') => void
  onRemoveItem: (cardId: number) => void
}

export interface ServicesConfigBlockProps {
  selectedFulfillmentId?: string
  selectedServices: string[]
  selectedConsumables: string[]
  organizationServices: { [orgId: string]: FulfillmentService[] }
  organizationSupplies: { [orgId: string]: FulfillmentService[] }
  deliveryDateOriginal: Date | undefined
  selectedFulfillmentOrg: string
  counterpartiesData: unknown
  onServiceToggle: (serviceId: string) => void
  onConsumableToggle: (consumableId: string) => void
  onDeliveryDateChange: (date: Date | undefined) => void
  onFulfillmentChange: (fulfillmentId: string) => void
}

export interface SupplierModalBlockProps {
  showSupplierModal: boolean
  newSupplier: NewSupplier
  supplierErrors: SupplierErrors
  creatingSupplier: boolean
  onClose: () => void
  onSupplierChange: (field: keyof NewSupplier, value: string) => void
  onCreateSupplier: () => void
}

// === ХУКИ ===

export interface UseWildberriesProductsReturn {
  searchTerm: string
  setSearchTerm: (term: string) => void
  loading: boolean
  wbCards: WildberriesCard[]
  loadCards: () => Promise<void>
  searchCards: () => Promise<void>
}

export interface UseSupplyManagementReturn {
  supplyItems: SupplyItem[]
  addToSupply: (card: WildberriesCard, quantity: number, supplierId: string) => void
  updateItemQuantity: (cardId: number, quantity: number) => void
  updateItemPrice: (cardId: number, price: number, priceType: 'perUnit' | 'total') => void
  removeItem: (cardId: number) => void
  getTotalItemsCost: () => number
  getTotalVolume: () => number
  getTotalQuantity: () => number
}

export interface UseFulfillmentServicesReturn {
  selectedServices: string[]
  selectedConsumables: string[]
  organizationServices: { [orgId: string]: FulfillmentService[] }
  organizationSupplies: { [orgId: string]: FulfillmentService[] }
  deliveryDateOriginal: Date | undefined
  selectedFulfillmentOrg: string
  toggleService: (serviceId: string) => void
  toggleConsumable: (consumableId: string) => void
  setDeliveryDateOriginal: (date: Date | undefined) => void
  setSelectedFulfillmentOrg: (orgId: string) => void
  loadOrganizationServices: (orgId: string) => Promise<void>
  loadOrganizationSupplies: (orgId: string) => Promise<void>
  getServicesCost: () => number
  getConsumablesCost: () => number
}

export interface UseSupplierFormReturn {
  suppliers: Supplier[]
  showSupplierModal: boolean
  newSupplier: NewSupplier
  supplierErrors: SupplierErrors
  setShowSupplierModal: (show: boolean) => void
  updateSupplier: (field: keyof NewSupplier, value: string) => void
  validateSupplier: () => boolean
  resetSupplierForm: () => void
  createSupplier: () => Promise<void>
}

export interface UseSupplyCreationReturn {
  deliveryDate: string
  selectedFulfillment: string
  goodsQuantity: number
  goodsVolume: number
  cargoPlaces: number
  goodsPrice: number
  fulfillmentServicesPrice: number
  logisticsPrice: number
  setDeliveryDate: (date: string) => void
  setSelectedFulfillment: (fulfillment: string) => void
  setGoodsQuantity: (quantity: number) => void
  setGoodsVolume: (volume: number) => void
  setCargoPlaces: (places: number) => void
  setGoodsPrice: (price: number) => void
  setFulfillmentServicesPrice: (price: number) => void
  setLogisticsPrice: (price: number) => void
  canCreateSupply: () => boolean
  createSupply: () => Promise<void>
}

// === УТИЛИТЫ И РАСЧЕТЫ ===

export interface VolumeCalculation {
  totalVolume: number
  itemsWithDimensions: number
  itemsWithoutDimensions: number
}

export interface CostBreakdown {
  itemsCost: number
  servicesCost: number
  consumablesCost: number
  logisticsCost: number
  totalCost: number
}

export interface SupplyValidation {
  hasItems: boolean
  hasServices: boolean
  hasDeliveryDate: boolean
  hasFulfillment: boolean
  isValid: boolean
  errors: string[]
}

// === МОКОВЫЕ ДАННЫЕ ===

export interface MockCardData {
  nmID: number
  vendorCode: string
  title: string
  description: string
  brand: string
  object: string
  parent: string
  countryProduction: string
  supplierVendorCode: string
  mediaFiles: string[]
  dimensions?: {
    length: number
    width: number
    height: number
    weightBrutto: number
    isValid: boolean
  }
  sizes: Array<{
    chrtID: number
    techSize: string
    wbSize: string
    price: number
    discountedPrice: number
    quantity: number
  }>
}

// === APOLLO/GRAPHQL ТИПЫ ===

export interface CreateSupplyVariables {
  fulfillmentCenterId: string
  deliveryDate: string
  items: Array<{
    cardId: string
    quantity: number
    price: number
  }>
  services: string[]
  consumables: string[]
}

export interface CreateSupplierVariables {
  name: string
  contactName: string
  phone: string
  market: string
  address: string
  place: string
  telegram: string
}
