// =============================================================================
// 📋 ХУК ДЛЯ УПРАВЛЕНИЯ ФОРМОЙ ПОСТАВКИ СЕЛЛЕРА
// =============================================================================

import { useState, useCallback } from 'react'

import type { 
  SellerConsumableSupplier,
  SelectedSellerConsumable,
  UseSellerSupplyFormReturn, 
} from '../types/seller-types'

export function useSellerSupplyForm(): UseSellerSupplyFormReturn {
  // 🏪 СОСТОЯНИЕ ПОСТАВЩИКА
  const [selectedSupplier, setSelectedSupplier] = useState<SellerConsumableSupplier | null>(null)
  
  // 🏢 СОСТОЯНИЕ ФУЛФИЛМЕНТ-ЦЕНТРА (специфично для селлера)
  const [selectedFulfillment, setSelectedFulfillment] = useState<SellerConsumableSupplier | null>(null)
  
  // 🚚 СОСТОЯНИЕ ЛОГИСТИКИ
  const [selectedLogistics, setSelectedLogistics] = useState<SellerConsumableSupplier | null>(null)
  
  // 📦 СОСТОЯНИЕ ВЫБРАННЫХ РАСХОДНИКОВ
  const [selectedConsumables, setSelectedConsumables] = useState<SelectedSellerConsumable[]>([])
  
  // 🔍 СОСТОЯНИЕ ПОИСКА
  const [searchQuery, setSearchQuery] = useState('')
  const [productSearchQuery, setProductSearchQuery] = useState('')
  
  // 📅 СОСТОЯНИЕ ДАТЫ ДОСТАВКИ
  const [deliveryDate, setDeliveryDate] = useState('')
  
  // 📝 СОСТОЯНИЕ ЗАМЕТОК
  const [notes, setNotes] = useState('')

  // 🔄 ФУНКЦИЯ СБРОСА ФОРМЫ
  const resetForm = useCallback(() => {
    setSelectedSupplier(null)
    setSelectedFulfillment(null)
    setSelectedLogistics(null)
    setSelectedConsumables([])
    setSearchQuery('')
    setProductSearchQuery('')
    setDeliveryDate('')
    setNotes('')
  }, [])

  // 🔧 СПЕЦИАЛЬНЫЕ СЕТТЕРЫ С ОЧИСТКОЙ ЗАВИСИМЫХ ПОЛЕЙ
  
  const handleSetSelectedSupplier = useCallback((supplier: SellerConsumableSupplier | null) => {
    setSelectedSupplier(supplier)
    
    // При смене поставщика очищаем выбранные товары
    setSelectedConsumables([])
    setProductSearchQuery('')
  }, [])

  const handleSetSelectedFulfillment = useCallback((fulfillment: SellerConsumableSupplier | null) => {
    setSelectedFulfillment(fulfillment)
    
    // При смене фулфилмента можем очистить логистику если нужно
    // setSelectedLogistics(null)
  }, [])

  const handleSetSelectedLogistics = useCallback((logistics: SellerConsumableSupplier | null) => {
    setSelectedLogistics(logistics)
  }, [])

  return {
    // Состояние
    selectedSupplier,
    selectedFulfillment,
    selectedLogistics,
    selectedConsumables,
    searchQuery,
    productSearchQuery,
    deliveryDate,
    notes,
    
    // Сеттеры
    setSelectedSupplier: handleSetSelectedSupplier,
    setSelectedFulfillment: handleSetSelectedFulfillment,
    setSelectedLogistics: handleSetSelectedLogistics,
    setSelectedConsumables,
    setSearchQuery,
    setProductSearchQuery,
    setDeliveryDate,
    setNotes,
    
    // Утилиты
    resetForm,
  }
}