import { queryOptions, useQuery } from '@tanstack/react-query';
import type { JobKind } from '../types/research';
import { researchApi, normalizeResearchSearchQuery } from './researchApi';

export const RESEARCH_CACHE_STALE_TIME_MS = 30_000;

export const researchQueryKeys = {
  all: ['research'] as const,
  workspaces: ['research', 'workspaces'] as const,
  snapshot: (workspaceId: string) => ['research', 'snapshot', workspaceId] as const,
  models: (workspaceId: string) => ['research', 'models', workspaceId] as const,
  model: (modelId: string) => ['research', 'model', modelId] as const,
  jobs: (workspaceId: string) => ['research', 'jobs', workspaceId] as const,
  files: (workspaceId: string) => ['research', 'files', workspaceId] as const,
  searchIdsPrefix: (workspaceId: string) =>
    ['research', 'searchIds', workspaceId] as const,
  searchIds: (workspaceId: string, normalizedQuery: string, showArchived: boolean) =>
    ['research', 'searchIds', workspaceId, normalizedQuery, showArchived] as const,
  auditTrail: (noteId: string) => ['research', 'auditTrail', noteId] as const,
};

export const researchWorkspacesQueryOptions = () =>
  queryOptions({
    queryKey: researchQueryKeys.workspaces,
    queryFn: () => researchApi.listWorkspaces(),
    staleTime: RESEARCH_CACHE_STALE_TIME_MS,
  });

export const researchSnapshotQueryOptions = (workspaceId: string) =>
  queryOptions({
    queryKey: researchQueryKeys.snapshot(workspaceId),
    queryFn: () => researchApi.getWorkspaceSnapshot(workspaceId),
    staleTime: RESEARCH_CACHE_STALE_TIME_MS,
  });

export const researchModelsQueryOptions = (workspaceId: string) =>
  queryOptions({
    queryKey: researchQueryKeys.models(workspaceId),
    queryFn: () => researchApi.listFinancialModels({ workspaceId }),
    staleTime: RESEARCH_CACHE_STALE_TIME_MS,
  });

export const researchModelQueryOptions = (modelId: string) =>
  queryOptions({
    queryKey: researchQueryKeys.model(modelId),
    queryFn: () => researchApi.getFinancialModel({ modelId }),
    staleTime: RESEARCH_CACHE_STALE_TIME_MS,
  });

export const researchJobsQueryOptions = (
  workspaceId: string,
  jobKind?: JobKind,
) =>
  queryOptions({
    queryKey: researchQueryKeys.jobs(workspaceId),
    queryFn: () => researchApi.listJobs({ workspaceId, jobKind }),
    staleTime: RESEARCH_CACHE_STALE_TIME_MS,
  });

export const researchSearchIdsQueryOptions = (
  workspaceId: string,
  query: string,
  showArchived: boolean,
) => {
  const normalizedQuery = normalizeResearchSearchQuery(query);
  return queryOptions({
    queryKey: researchQueryKeys.searchIds(workspaceId, normalizedQuery, showArchived),
    queryFn: () =>
      researchApi.searchNoteIds({
        workspaceId,
        query: normalizedQuery,
        includeArchived: showArchived,
      }),
    staleTime: 0,
  });
};

export const researchAuditTrailQueryOptions = (noteId: string) =>
  queryOptions({
    queryKey: researchQueryKeys.auditTrail(noteId),
    queryFn: () => researchApi.getNoteAuditTrail({ noteId }),
    staleTime: 0,
  });

export const researchFilesQueryOptions = (workspaceId: string) =>
  queryOptions({
    queryKey: researchQueryKeys.files(workspaceId),
    queryFn: () => researchApi.listFiles({ workspaceId }),
    staleTime: RESEARCH_CACHE_STALE_TIME_MS,
  });

export const useResearchWorkspacesQuery = () => useQuery(researchWorkspacesQueryOptions());

export const useResearchSnapshotQuery = (workspaceId: string | null) =>
  useQuery({
    ...(workspaceId ? researchSnapshotQueryOptions(workspaceId) : researchSnapshotQueryOptions('__disabled__')),
    enabled: Boolean(workspaceId),
  });

export const useResearchModelsQuery = (workspaceId: string | null) =>
  useQuery({
    ...(workspaceId ? researchModelsQueryOptions(workspaceId) : researchModelsQueryOptions('__disabled__')),
    enabled: Boolean(workspaceId),
  });

export const useResearchJobsQuery = (workspaceId: string | null) =>
  useQuery({
    ...(workspaceId ? researchJobsQueryOptions(workspaceId) : researchJobsQueryOptions('__disabled__')),
    enabled: Boolean(workspaceId),
  });

export const useResearchSearchIdsQuery = (
  workspaceId: string | null,
  query: string,
  showArchived: boolean,
) => {
  const normalizedQuery = normalizeResearchSearchQuery(query);
  return useQuery({
    ...(workspaceId
      ? researchSearchIdsQueryOptions(workspaceId, normalizedQuery, showArchived)
      : researchSearchIdsQueryOptions('__disabled__', normalizedQuery, showArchived)),
    enabled: Boolean(workspaceId && normalizedQuery),
  });
};

export const useResearchAuditTrailQuery = (noteId: string | null) =>
  useQuery({
    ...(noteId ? researchAuditTrailQueryOptions(noteId) : researchAuditTrailQueryOptions('__disabled__')),
    enabled: Boolean(noteId),
  });

export const useResearchFilesQuery = (workspaceId: string | null) =>
  useQuery({
    ...(workspaceId ? researchFilesQueryOptions(workspaceId) : researchFilesQueryOptions('__disabled__')),
    enabled: Boolean(workspaceId),
  });
