import { queryOptions } from '@tanstack/react-query';
import type { CrmSharedReportFilters } from './types';
import {
  getCrmOpportunityAgingReport,
  getCrmLeadAgingReport,
  getCrmPipelineReport,
  getCrmReportCatalog,
  getCrmReportFilters,
  getCrmReports
} from './service';

export const crmReportKeys = {
  all: ['crm-reports'] as const,
  list: () => [...crmReportKeys.all, 'list'] as const,
  catalog: () => [...crmReportKeys.all, 'catalog'] as const,
  filters: () => [...crmReportKeys.all, 'filters'] as const,
  pipelineRoot: () => [...crmReportKeys.all, 'pipeline'] as const,
  pipeline: (filters: CrmSharedReportFilters) => [...crmReportKeys.pipelineRoot(), filters] as const,
  leadAgingRoot: () => [...crmReportKeys.all, 'lead-aging'] as const,
  leadAging: (filters: CrmSharedReportFilters) => [...crmReportKeys.leadAgingRoot(), filters] as const,
  opportunityAgingRoot: () => [...crmReportKeys.all, 'opportunity-aging'] as const,
  opportunityAging: (filters: CrmSharedReportFilters) => [...crmReportKeys.opportunityAgingRoot(), filters] as const
};

export const crmReportsQueryOptions = () =>
  queryOptions({
    queryKey: crmReportKeys.list(),
    queryFn: () => getCrmReports()
  });

export const crmReportCatalogQueryOptions = () =>
  queryOptions({
    queryKey: crmReportKeys.catalog(),
    queryFn: () => getCrmReportCatalog()
  });

export const crmReportFiltersQueryOptions = () =>
  queryOptions({
    queryKey: crmReportKeys.filters(),
    queryFn: () => getCrmReportFilters()
  });

export const crmPipelineReportQueryOptions = (filters: CrmSharedReportFilters) =>
  queryOptions({
    queryKey: crmReportKeys.pipeline(filters),
    queryFn: () => getCrmPipelineReport(filters)
  });

export const crmLeadAgingReportQueryOptions = (filters: CrmSharedReportFilters) =>
  queryOptions({
    queryKey: crmReportKeys.leadAging(filters),
    queryFn: () => getCrmLeadAgingReport(filters)
  });

export const crmOpportunityAgingReportQueryOptions = (filters: CrmSharedReportFilters) =>
  queryOptions({
    queryKey: crmReportKeys.opportunityAging(filters),
    queryFn: () => getCrmOpportunityAgingReport(filters)
  });

