import { useState, useCallback, useEffect } from 'react'
import { api } from '../api/client'
import type { UserOut, UserListItem, VacationBalanceOut } from '../types/absence'
import { MANAGER_ROLES } from '../utils/calendar'

export function usePlanerView(user: UserOut | null, colleagues: UserListItem[], year: number) {
  const [viewMode, setViewMode] = useState<'liste' | 'planer'>('liste')
  const [planMonth, setPlanMonth] = useState(new Date().getMonth())
  const [colleagueBalances, setColleagueBalances] = useState<Record<string, VacationBalanceOut>>({})
  const [balancesLoaded, setBalancesLoaded] = useState(false)
  const [showYearGrid, setShowYearGrid] = useState<boolean>(() => {
    try { return localStorage.getItem('planer_showYearGrid') !== 'false' }
    catch { return true }
  })

  useEffect(() => {
    try { localStorage.setItem('planer_showYearGrid', String(showYearGrid)) }
    catch { /* ignore */ }
  }, [showYearGrid])

  const loadColleagueBalances = useCallback(async (list: UserListItem[]) => {
    if (list.length === 0 || balancesLoaded) return
    try {
      const results = await Promise.allSettled(
        list.map(c => api.get<VacationBalanceOut>(`/absences/balance/${c.id}?year=${year}`))
      )
      const map: Record<string, VacationBalanceOut> = {}
      results.forEach((r, i) => { if (r.status === 'fulfilled') map[list[i].id] = r.value })
      setColleagueBalances(map)
      setBalancesLoaded(true)
    } catch { /* ignore */ }
  }, [year, balancesLoaded])

  useEffect(() => {
    if (viewMode === 'planer' && user && MANAGER_ROLES.includes(user.role) && colleagues.length > 0 && !balancesLoaded) {
      loadColleagueBalances(colleagues)
    }
  }, [viewMode, user, colleagues, balancesLoaded, loadColleagueBalances])

  return {
    viewMode,
    setViewMode,
    planMonth,
    setPlanMonth,
    colleagueBalances,
    balancesLoaded,
    showYearGrid,
    setShowYearGrid,
  }
}
