import { queryOptions } from '@tanstack/react-query';
import { getDocumentTemplateById, getDocumentTemplates, getDocumentTemplateVersions } from './service';
import type { DocumentTemplateFilters } from './types';

export const documentTemplateKeys = {
  all: ['crm-document-templates'] as const,
  lists: () => [...documentTemplateKeys.all, 'list'] as const,
  list: (filters: DocumentTemplateFilters) =>
    [...documentTemplateKeys.lists(), filters] as const,
  details: () => [...documentTemplateKeys.all, 'detail'] as const,
  detail: (id: string) => [...documentTemplateKeys.details(), id] as const,
  versionsRoot: () => [...documentTemplateKeys.all, 'versions'] as const,
  versions: (id: string) => [...documentTemplateKeys.versionsRoot(), id] as const
};

export const documentTemplatesQueryOptions = (filters: DocumentTemplateFilters = {}) =>
  queryOptions({
    queryKey: documentTemplateKeys.list(filters),
    queryFn: () => getDocumentTemplates(filters)
  });

export const documentTemplateByIdOptions = (id: string) =>
  queryOptions({
    queryKey: documentTemplateKeys.detail(id),
    queryFn: () => getDocumentTemplateById(id)
  });

export const documentTemplateVersionsQueryOptions = (id: string) =>
  queryOptions({
    queryKey: documentTemplateKeys.versions(id),
    queryFn: () => getDocumentTemplateVersions(id)
  });
