import { useState, useEffect, useCallback } from 'react' import * as api from '@/services/api' import type { BreakPeriod } from '@/types' interface UseBreakPeriodsResult { data: BreakPeriod[] | null isLoading: boolean error: string | null refresh: () => Promise add: ( startDate: string, endDate: string, name: string, targetWeeks: number ) => Promise remove: (id: string) => Promise } export function useBreakPeriods(workspaceId: string | null): UseBreakPeriodsResult { const [data, setData] = useState(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const fetch = useCallback(async () => { if (!workspaceId) return setIsLoading(true) setError(null) try { const response = await api.getBreakPeriods(workspaceId) setData(response) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch break periods') setData(null) } finally { setIsLoading(false) } }, [workspaceId]) const add = useCallback( async (startDate: string, endDate: string, name: string, targetWeeks: number) => { if (!workspaceId) throw new Error('Workspace not selected') const result = await api.createBreakPeriod(workspaceId, { name, startDate, endDate, targetWeeks, }) await fetch() return result }, [workspaceId, fetch] ) const remove = useCallback( async (id: string) => { await api.deleteBreakPeriod(id) await fetch() }, [fetch] ) useEffect(() => { fetch() }, [fetch]) return { data, isLoading, error, refresh: fetch, add, remove } }