import { useCallback, useState } from 'react'

import type { CampaignStats, DailyAdvertisingData, UseDataProcessingReturn } from '../types'

/**
 * Хук для преобразования и обработки данных кампаний
 */
export function useDataProcessing(): UseDataProcessingReturn {
  const [dailyData, setDailyData] = useState<DailyAdvertisingData[]>([])

  const getDateRange = useCallback((
    selectedPeriod: string,
    useCustomDates: boolean,
    startDate: string,
    endDate: string,
  ) => {
    if (useCustomDates && startDate && endDate) {
      return { 
        startDate: new Date(startDate), 
        endDate: new Date(endDate),
      }
    }

    const endDateCalc = new Date()
    const startDateCalc = new Date()

    switch (selectedPeriod) {
      case 'week':
        startDateCalc.setDate(endDateCalc.getDate() - 7)
        break
      case 'month':
        startDateCalc.setMonth(endDateCalc.getMonth() - 1)
        break
      case 'quarter':
        startDateCalc.setMonth(endDateCalc.getMonth() - 3)
        break
      default:
        // По умолчанию неделя
        startDateCalc.setDate(endDateCalc.getDate() - 7)
    }

    return {
      startDate: startDateCalc,
      endDate: endDateCalc,
    }
  }, [])

  const convertCampaignDataToDailyData = useCallback((
    campaigns: CampaignStats[], 
    externalAdsData?: any,
  ) => {
    const dailyMap = new Map<string, DailyAdvertisingData>()

    // Обрабатываем данные кампаний Wildberries
    campaigns.forEach((campaign) => {
      if ((campaign as any).days) {
        (campaign as any).days.forEach((day: any) => {
          const dateKey = day.date.split('T')[0] // Получаем только дату без времени

          if (!dailyMap.has(dateKey)) {
            dailyMap.set(dateKey, {
              date: dateKey,
              totalSum: 0,
              totalOrders: 0,
              totalRevenue: 0,
              products: [],
            })
          }

          const dailyRecord = dailyMap.get(dateKey)!

          // Добавляем товары с их рекламными кампаниями
          if (day.apps) {
            day.apps.forEach((app: any) => {
              if (app.nm) {
                app.nm.forEach((product: any) => {
                  let existingProduct = dailyRecord.products.find((p) => p.nmId === product.nmId)

                  if (!existingProduct) {
                    // Создаем новый товар
                    existingProduct = {
                      nmId: product.nmId,
                      name: product.name || `Товар ${product.nmId}`,
                      totalViews: 0,
                      totalClicks: 0,
                      totalCost: 0,
                      totalOrders: 0,
                      totalRevenue: 0,
                      advertising: {
                        wbCampaigns: [],
                        externalAds: [],
                      },
                    }
                    dailyRecord.products.push(existingProduct)
                  }

                  // Суммируем данные товара
                  existingProduct.totalViews += product.views || 0
                  existingProduct.totalClicks += product.clicks || 0
                  existingProduct.totalCost += product.sum || 0
                  existingProduct.totalOrders += product.orders || 0
                  existingProduct.totalRevenue += product.sum || 0 // Для простоты используем sum как revenue

                  // Добавляем кампанию WB
                  existingProduct.advertising.wbCampaigns.push({
                    campaignId: campaign.campaignId,
                    views: product.views || 0,
                    clicks: product.clicks || 0,
                    cost: product.sum || 0,
                    orders: product.orders || 0,
                  })

                  // Суммируем общие данные дня
                  dailyRecord.totalSum += product.sum || 0
                  dailyRecord.totalOrders += product.orders || 0
                  dailyRecord.totalRevenue += product.sum || 0
                })
              }
            })
          }
        })
      }
    })

    // Добавляем данные внешней рекламы, если есть
    if (externalAdsData?.getExternalAds) {
      externalAdsData.getExternalAds.forEach((ad: any) => {
        const adDate = new Date(ad.date).toISOString().split('T')[0]
        
        if (!dailyMap.has(adDate)) {
          dailyMap.set(adDate, {
            date: adDate,
            totalSum: 0,
            totalOrders: 0,
            totalRevenue: 0,
            products: [],
          })
        }

        const dailyRecord = dailyMap.get(adDate)!
        
        // Добавляем внешнюю рекламу к соответствующему товару или создаем общую запись
        let targetProduct = dailyRecord.products.find(p => p.nmId === ad.nmId)
        
        if (!targetProduct && ad.nmId) {
          targetProduct = {
            nmId: ad.nmId,
            name: `Товар ${ad.nmId}`,
            totalViews: 0,
            totalClicks: 0,
            totalCost: 0,
            totalOrders: 0,
            totalRevenue: 0,
            advertising: {
              wbCampaigns: [],
              externalAds: [],
            },
          }
          dailyRecord.products.push(targetProduct)
        }

        if (targetProduct) {
          targetProduct.advertising.externalAds.push({
            id: ad.id,
            name: ad.name,
            url: ad.url,
            cost: ad.cost,
            clicks: ad.clicks,
          })

          targetProduct.totalCost += ad.cost
          dailyRecord.totalSum += ad.cost
        }
      })
    }

    // Преобразуем Map в массив и сортируем по дате
    const result = Array.from(dailyMap.values()).sort(
      (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(),
    )

    setDailyData(result)
  }, [])

  return {
    dailyData,
    convertCampaignDataToDailyData,
    getDateRange,
  }
}