import { queryOptions } from '@tanstack/react-query';
import {
  getEnquiries,
  getEnquiryById,
  getEnquiryFollowups,
  getEnquiryProjectParties
} from './service';
import type { EnquiryFilters } from './types';

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

export const enquiriesQueryOptions = (filters: EnquiryFilters) =>
  queryOptions({
    queryKey: enquiryKeys.list(filters),
    queryFn: () => getEnquiries(filters)
  });

export const enquiryByIdOptions = (id: string) =>
  queryOptions({
    queryKey: enquiryKeys.detail(id),
    queryFn: () => getEnquiryById(id)
  });

export const enquiryProjectPartiesOptions = (id: string) =>
  queryOptions({
    queryKey: enquiryKeys.projectParties(id),
    queryFn: () => getEnquiryProjectParties(id)
  });

export const enquiryFollowupsOptions = (id: string) =>
  queryOptions({
    queryKey: enquiryKeys.followups(id),
    queryFn: () => getEnquiryFollowups(id)
  });
