import { useCallback, useMemo, useState } from 'react'

import { MOCK_EMPLOYEES } from '../constants'
import type { CalendarDay, Employee, UseEmployeeManagementReturn } from '../types'

/**
 * Хук для управления сотрудниками и генерации их календарных данных
 */
export function useEmployeeManagement(): UseEmployeeManagementReturn {
  const [employees, setEmployees] = useState<Employee[]>(MOCK_EMPLOYEES)

  const selectedEmployee = useMemo(() => {
    return employees.find(emp => emp.id === 'employee1') || employees[0]
  }, [employees])

  const addEmployee = useCallback((employee: Employee) => {
    setEmployees(prev => [...prev, employee])
  }, [])

  const removeEmployee = useCallback((employeeId: string) => {
    setEmployees(prev => prev.filter(emp => emp.id !== employeeId))
  }, [])

  const updateEmployee = useCallback((employeeId: string, updates: Partial<Employee>) => {
    setEmployees(prev => 
      prev.map(emp => 
        emp.id === employeeId ? { ...emp, ...updates } : emp,
      ),
    )
  }, [])

  const generateEmployeeCalendarData = useCallback((
    employeeId: string, 
    month: number, 
    year: number,
  ): CalendarDay[] => {
    const employee = employees.find(emp => emp.id === employeeId)
    if (!employee) return []

    const daysInMonth = new Date(year, month + 1, 0).getDate()
    const calendarData: CalendarDay[] = []

    for (let day = 1; day <= daysInMonth; day++) {
      const dayOfWeek = new Date(year, month, day).getDay()
      const isWeekend = dayOfWeek === 0 || dayOfWeek === 6

      // Генерируем реалистичные данные на основе профиля сотрудника
      let status = 'work'
      let hours = 8
      let overtime = 0
      let workType = 'office'
      let mood = 'good'
      const efficiency = employee.efficiency + Math.floor(Math.random() * 10) - 5
      const tasks = Math.floor(Math.random() * 8) + 2
      const breaks = Math.floor(Math.random() * 4) + 1

      // Выходные дни
      if (isWeekend) {
        status = Math.random() > 0.8 ? 'work' : 'weekend'
        hours = status === 'work' ? Math.floor(Math.random() * 6) + 2 : 0
        overtime = status === 'work' ? Math.floor(Math.random() * 4) : 0
      } else {
        // Рабочие дни - иногда отпуск/больничный
        const rand = Math.random()
        if (rand > 0.95) {
          status = 'sick'
          hours = 0
        } else if (rand > 0.9) {
          status = 'vacation'
          hours = 0
        } else if (rand > 0.85) {
          status = 'business'
          hours = Math.floor(Math.random() * 4) + 6
          workType = 'business_trip'
        } else if (rand > 0.7) {
          status = 'remote'
          workType = 'remote'
          hours = Math.floor(Math.random() * 3) + 7
        }

        // Переработки для активных сотрудников
        if (status === 'work' && employee.level === 'Senior' || employee.level === 'Lead') {
          overtime = Math.random() > 0.7 ? Math.floor(Math.random() * 3) + 1 : 0
        }
      }

      // Настроение зависит от нагрузки
      const totalWorkload = hours + overtime
      if (totalWorkload > 10) {
        mood = Math.random() > 0.5 ? 'tired' : 'normal'
      } else if (totalWorkload === 0) {
        mood = Math.random() > 0.5 ? 'excellent' : 'good'
      }

      calendarData.push({
        day,
        status,
        hours,
        overtime,
        workType,
        mood,
        efficiency: Math.max(0, Math.min(100, efficiency)),
        tasks,
        breaks,
      })
    }

    return calendarData
  }, [employees])

  return {
    employees,
    selectedEmployee,
    addEmployee,
    removeEmployee,
    updateEmployee,
    generateEmployeeCalendarData,
  }
}