import { useQuery } from '@tanstack/react-query';
import api from '@/lib/api-client';

type PlanPeriod = { id: number; label: string; is_active: number | boolean };

export function usePlanPeriods() {
  const query = useQuery({
    queryKey: ['plan-periods'],
    queryFn: async () => (await api.get<{ data: PlanPeriod[] }>('/plan-periods', { params: { per_page: 100 } })).data,
    refetchInterval: 60_000,
  });
  const periods = query.data?.data ?? [];
  return { periods, activePeriod: periods.find(period => Number(period.is_active) === 1), loading: query.isLoading };
}
