import { queryOptions } from '@tanstack/react-query';
import type { OpportunityFilters } from './types';
import {
  getOpportunityAttachments,
  getOpportunityById,
  getOpportunityFollowups,
  getOpportunityProjectParties,
  getOpportunities
} from './service';

export const opportunityKeys = {
  all: ['crm-opportunities'] as const,
  lists: () => [...opportunityKeys.all, 'list'] as const,
  list: (filters: OpportunityFilters) => [...opportunityKeys.lists(), filters] as const,
  details: () => [...opportunityKeys.all, 'detail'] as const,
  detail: (id: string) => [...opportunityKeys.details(), id] as const,
  projectPartiesRoot: () => [...opportunityKeys.all, 'project-parties'] as const,
  projectParties: (id: string) => [...opportunityKeys.projectPartiesRoot(), id] as const,
  followupsRoot: () => [...opportunityKeys.all, 'followups'] as const,
  followups: (id: string) => [...opportunityKeys.followupsRoot(), id] as const,
  attachmentsRoot: () => [...opportunityKeys.all, 'attachments'] as const,
  attachments: (id: string) => [...opportunityKeys.attachmentsRoot(), id] as const
};

export const opportunitiesQueryOptions = (filters: OpportunityFilters) =>
  queryOptions({
    queryKey: opportunityKeys.list(filters),
    queryFn: () => getOpportunities(filters)
  });

export const opportunityByIdOptions = (id: string) =>
  queryOptions({
    queryKey: opportunityKeys.detail(id),
    queryFn: () => getOpportunityById(id)
  });

export const opportunityProjectPartiesOptions = (id: string) =>
  queryOptions({
    queryKey: opportunityKeys.projectParties(id),
    queryFn: () => getOpportunityProjectParties(id)
  });

export const opportunityFollowupsOptions = (id: string) =>
  queryOptions({
    queryKey: opportunityKeys.followups(id),
    queryFn: () => getOpportunityFollowups(id)
  });

export const opportunityAttachmentsOptions = (id: string) =>
  queryOptions({
    queryKey: opportunityKeys.attachments(id),
    queryFn: () => getOpportunityAttachments(id)
  });
