import { CheckCircle } from 'lucide-react'
import { useCallback } from 'react'

import { MONTH_NAMES, MOOD_TYPES, WORK_STATUSES, WORK_TYPES } from '../constants'
import type { UseTimesheetUtilsReturn } from '../types'

/**
 * Хук с утилитами для табеля учета времени
 */
export function useTimesheetUtils(): UseTimesheetUtilsReturn {
  const getStatusColor = useCallback((status: string): string => {
    const statusConfig = WORK_STATUSES.find(s => s.key === status)
    return statusConfig?.color || 'bg-gray-100 border-gray-200 text-gray-500'
  }, [])

  const getStatusIcon = useCallback((status: string) => {
    const statusConfig = WORK_STATUSES.find(s => s.key === status)
    return statusConfig?.icon || CheckCircle
  }, [])

  const getMoodIcon = useCallback((mood: string | null) => {
    if (!mood) return null
    const moodConfig = MOOD_TYPES.find(m => m.key === mood)
    return moodConfig?.icon || null
  }, [])

  const getWorkTypeIcon = useCallback((workType: string | null) => {
    if (!workType) return null
    const workTypeConfig = WORK_TYPES.find(w => w.key === workType)
    return workTypeConfig?.icon || null
  }, [])

  const formatHours = useCallback((hours: number): string => {
    if (hours === 0) return '0ч'
    if (hours < 1) return `${Math.round(hours * 60)}м`
    return `${hours}ч`
  }, [])

  const formatEfficiency = useCallback((efficiency: number | null): string => {
    if (efficiency === null) return 'N/A'
    return `${efficiency}%`
  }, [])

  const getMonthName = useCallback((month: number): string => {
    return MONTH_NAMES[month] || 'Неизвестный месяц'
  }, [])

  const getDaysInMonth = useCallback((month: number, year: number): number => {
    return new Date(year, month + 1, 0).getDate()
  }, [])

  const getFirstDayOfMonth = useCallback((month: number, year: number): number => {
    return new Date(year, month, 1).getDay()
  }, [])

  return {
    getStatusColor,
    getStatusIcon,
    getMoodIcon,
    getWorkTypeIcon,
    formatHours,
    formatEfficiency,
    getMonthName,
    getDaysInMonth,
    getFirstDayOfMonth,
  }
}