"use client";

import { useDashboardStatisticsQuery } from "@/hooks/queries";
import { useMemo, useState } from "react";
import { toLabeledPoints } from "./to-labeled-points";

export const STATISTICS_DAY_OPTIONS = [7, 14, 30, 90] as const;

export type StatisticsDays = (typeof STATISTICS_DAY_OPTIONS)[number];

export function useStatistics() {
  const [days, setDays] = useState<StatisticsDays>(14);

  const query = useDashboardStatisticsQuery({ days });
  const stats = query.data?.data;

  const daily = stats?.daily;

  const scrapedTed = useMemo(
    () => toLabeledPoints(daily?.scraped_ted ?? []),
    [daily],
  );
  const scrapedDocuments = useMemo(
    () => toLabeledPoints(daily?.scraped_documents ?? []),
    [daily],
  );
  const translatedNotices = useMemo(
    () => toLabeledPoints(daily?.translated_notices ?? []),
    [daily],
  );

  return {
    days,
    setDays,
    isLoading: query.isPending,
    isError: query.isError,
    refetch: query.refetch,
    scrapedTed,
    scrapedDocuments,
    translatedNotices,
    totalScrapedDocuments: stats?.totals.scraped_documents ?? 0,
    totalTranslatedTenders: stats?.totals.translated_tenders ?? 0,
  };
}
