import { queryOptions } from '@tanstack/react-query';
import {
  getApprovalById,
  getApprovalWorkflowById,
  getApprovalWorkflows,
  getApprovals
} from './service';
import type { ApprovalFilters } from './types';

export const approvalKeys = {
  all: ['crm-approvals'] as const,
  lists: () => [...approvalKeys.all, 'list'] as const,
  list: (filters: ApprovalFilters) => [...approvalKeys.lists(), filters] as const,
  details: () => [...approvalKeys.all, 'detail'] as const,
  detail: (id: string) => [...approvalKeys.details(), id] as const
};

export const approvalWorkflowKeys = {
  all: ['crm-approval-workflows'] as const,
  lists: () => [...approvalWorkflowKeys.all, 'list'] as const,
  list: () => [...approvalWorkflowKeys.lists(), 'all'] as const,
  details: () => [...approvalWorkflowKeys.all, 'detail'] as const,
  detail: (id: string) => [...approvalWorkflowKeys.details(), id] as const
};

export const approvalsQueryOptions = (filters: ApprovalFilters) =>
  queryOptions({
    queryKey: approvalKeys.list(filters),
    queryFn: () => getApprovals(filters)
  });

export const approvalByIdOptions = (id: string) =>
  queryOptions({
    queryKey: approvalKeys.detail(id),
    queryFn: () => getApprovalById(id)
  });

export const approvalWorkflowsQueryOptions = () =>
  queryOptions({
    queryKey: approvalWorkflowKeys.list(),
    queryFn: () => getApprovalWorkflows()
  });

export const approvalWorkflowByIdOptions = (id: string) =>
  queryOptions({
    queryKey: approvalWorkflowKeys.detail(id),
    queryFn: () => getApprovalWorkflowById(id)
  });
