import { useCallback, useState } from 'react'

import type { CalendarDay, TimesheetVariant, UseTimesheetStateReturn } from '../types'

/**
 * Хук для управления основным состоянием табеля учета времени
 */
export function useTimesheetState(): UseTimesheetStateReturn {
  const [selectedVariant, setSelectedVariant] = useState<TimesheetVariant>('galaxy')
  const [selectedEmployee, setSelectedEmployee] = useState('employee1')
  const [selectedMonth, setSelectedMonth] = useState(new Date().getMonth())
  const [selectedYear, setSelectedYear] = useState(new Date().getFullYear())
  const [animatedStats, setAnimatedStats] = useState(false)
  const [calendarData, setCalendarData] = useState<CalendarDay[]>([])
  const [editableCalendarData, setEditableCalendarData] = useState<CalendarDay[]>([])

  const handleMonthChange = useCallback((direction: 'prev' | 'next') => {
    if (direction === 'prev') {
      if (selectedMonth === 0) {
        setSelectedMonth(11)
        setSelectedYear(prev => prev - 1)
      } else {
        setSelectedMonth(prev => prev - 1)
      }
    } else {
      if (selectedMonth === 11) {
        setSelectedMonth(0)
        setSelectedYear(prev => prev + 1)
      } else {
        setSelectedMonth(prev => prev + 1)
      }
    }
  }, [selectedMonth])

  return {
    selectedVariant,
    selectedEmployee,
    selectedMonth,
    selectedYear,
    calendarData,
    editableCalendarData,
    animatedStats,
    setSelectedVariant,
    setSelectedEmployee,
    setSelectedMonth,
    setSelectedYear,
    setCalendarData,
    setEditableCalendarData,
    setAnimatedStats,
    handleMonthChange,
  }
}