'use client';

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

import { apiFetch } from '@/lib/api/client';
import { isCustomRange, type DateRange } from '@/lib/analytics/range';
import type {
  LeadSourceAttributionData,
  MetricBase,
  OccupancyTimelineData,
  PipelineFunnelData,
} from '@/lib/services/analytics.service';

interface MetricResponse<T> {
  metric: MetricBase;
  range: DateRange;
  data: T;
}

/**
 * Serialize a DateRange (preset or custom) into the URL query params the
 * /api/v1/analytics route expects: `range=30d` for presets, or
 * `range=custom&from=YYYY-MM-DD&to=YYYY-MM-DD` for custom.
 */
function rangeToQuery(range: DateRange): string {
  if (isCustomRange(range)) {
    return `range=custom&from=${range.from}&to=${range.to}`;
  }
  return `range=${range}`;
}

export function useAnalyticsMetric<T>(metric: MetricBase, range: DateRange) {
  return useQuery<T>({
    // Stringify custom ranges into the cache key so React Query treats
    // each {from,to} pair as its own query - otherwise switching dates
    // would never refetch.
    queryKey: ['analytics', metric, range],
    queryFn: async () => {
      const res = await apiFetch<MetricResponse<T>>(
        `/api/v1/analytics?metric=${metric}&${rangeToQuery(range)}`,
      );
      return res.data;
    },
    staleTime: 60_000,
    retry: 2,
  });
}

export const useFunnel = (range: DateRange) =>
  useAnalyticsMetric<PipelineFunnelData>('pipeline_funnel', range);
export const useOccupancy = (range: DateRange) =>
  useAnalyticsMetric<OccupancyTimelineData>('occupancy_timeline', range);
export const useLeadSource = (range: DateRange) =>
  useAnalyticsMetric<LeadSourceAttributionData>('lead_source_attribution', range);
