import { useQuery } from '@tanstack/react-query';
import { api, type ApiResponse } from '../lib/api';
import type { AccuracyRow, ReportEnvelope, UtilizationRow } from '../types';

/**
 * گزارش‌های بهره‌وری و دقت برنامه.
 *
 * بازه همیشه صریح فرستاده می‌شود تا نمودار با پیش‌فرض سرور جابه‌جا نشود.
 */
export function useResourceUtilization(branchUuid: string | undefined, from: number, to: number) {
  const query = useQuery({
    queryKey: ['resource-utilization', branchUuid, from, to],
    queryFn: () =>
      api.get<ApiResponse<ReportEnvelope<UtilizationRow>>>(
        `/api/v1/reports/resource-utilization?branch_uuid=${branchUuid}&from=${from}&to=${to}`,
      ),
    enabled: !!branchUuid,
  });

  return { rows: query.data?.data?.rows ?? [], loading: query.isLoading };
}

export function usePlanAccuracy(from: number, to: number) {
  const query = useQuery({
    queryKey: ['plan-accuracy', from, to],
    queryFn: () =>
      api.get<ApiResponse<ReportEnvelope<AccuracyRow>>>(
        `/api/v1/reports/plan-accuracy?from=${from}&to=${to}`,
      ),
  });

  return { rows: query.data?.data?.rows ?? [], loading: query.isLoading };
}
