import { queryOptions } from "@tanstack/react-query";
import { jsonQuery } from "../apiAdapter";

export const dashboardOptions = () =>
  queryOptions({
    queryKey: ["dashboard"],
    queryFn: () => jsonQuery<Record<string, unknown>>("/api/admin/dashboard"),
    staleTime: 60_000,
    // The dashboard aggregates MANY domains (attendance, offers, invoices,
    // orders, projects, leave). Mutations in those domains can't all be
    // expected to invalidate ["dashboard"], so always refetch on mount —
    // navigating back to the dashboard must never show pre-mutation data.
    refetchOnMount: "always",
  });

// require2FAOptions lives in ./settings.ts (the single definition consumers
// import). The duplicate copy that used to be here was dead.

export interface Session {
  id: number | string;
  is_current: boolean;
  device_info?: {
    icon?: string;
    browser?: string;
    os?: string;
  };
  ip_address: string;
  created_at: string;
}

export const sessionsOptions = () =>
  queryOptions({
    queryKey: ["sessions"],
    queryFn: async (): Promise<Session[]> => {
      // Route through jsonQuery for the shared response.ok / 401 / non-JSON
      // guards, then normalize the two possible payload shapes (a bare array
      // or `{ sessions: [...] }`).
      const data = await jsonQuery<Session[] | { sessions?: Session[] }>(
        "/api/admin/sessions",
      );
      return Array.isArray(data) ? data : (data.sessions ?? []);
    },
  });
