"use client";

import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import type { TDatasetFieldsStatus } from "@/modules/ee/analysis/charts/hooks/chart-field-catalog-query";
import { listDashboardFilterFields } from "@/modules/ee/analysis/lib/api-client";
import type { TEmbeddedChartField } from "@/modules/ee/analysis/lib/field-catalog";
import { dashboardKeys } from "@/modules/ee/analysis/lib/query";

export type TDashboardEmbeddedFields = TDatasetFieldsStatus & {
  embeddedFields: TEmbeddedChartField[];
  /** The keys that are numbers on this dashboard (`type: "number"`), by member. */
  numericFields: ReadonlySet<string>;
};

/** How long the app reuses a dashboard's filter fields; the server answers from its own cached key lists. */
const FILTER_FIELDS_STALE_MS = 5 * 60 * 1000;

const NO_FIELDS: TEmbeddedChartField[] = [];
const NO_NUMERIC_FIELDS: ReadonlySet<string> = new Set();

type TSelectedFields = { embeddedFields: TEmbeddedChartField[]; numericFields: ReadonlySet<string> };

/**
 * The embedded-data fields a dashboard filter can offer: every key of every Feedback Dataset the
 * dashboard's charts read, once each, from the dashboard-bound `GET /api/v3/dashboards/{id}/filter-fields`
 * — so a Dashboard viewer, whom the field catalog refuses, gets them too. `datasetIds` are the charts'
 * datasets: a change to them reads the fields again. `numericFields` are the keys that are numbers on the
 * dashboard. Empty until loaded, or while `enabled` is off.
 */
export const useDashboardEmbeddedFields = ({
  dashboardId,
  datasetIds,
  enabled = true,
}: Readonly<{
  dashboardId: string;
  datasetIds: readonly string[];
  enabled?: boolean;
}>): TDashboardEmbeddedFields => {
  const uniqueIds = useMemo(() => [...new Set(datasetIds)].sort((a, b) => a.localeCompare(b)), [datasetIds]);
  const query = useQuery({
    queryKey: dashboardKeys.filterFields(dashboardId, uniqueIds),
    queryFn: ({ signal }) => listDashboardFilterFields(dashboardId, signal),
    // A dashboard without charts has no dataset to offer keys from.
    enabled: enabled && dashboardId.length > 0 && uniqueIds.length > 0,
    staleTime: FILTER_FIELDS_STALE_MS,
    select: (data): TSelectedFields => ({
      // A label equal to the key carries no rename; keep it off so the key stays the fallback.
      embeddedFields: data.fields.map(({ name, key, label }) => ({
        name,
        key,
        ...(label && label !== key ? { label } : {}),
      })),
      numericFields: new Set(data.fields.flatMap(({ name, type }) => (type === "number" ? [name] : []))),
    }),
  });
  const { refetch } = query;
  return {
    embeddedFields: query.data?.embeddedFields ?? NO_FIELDS,
    numericFields: query.data?.numericFields ?? NO_NUMERIC_FIELDS,
    isLoading: query.isLoading,
    error: query.error ?? null,
    retry: () => void refetch(),
  };
};
