import { queryOptions } from '@tanstack/react-query';
import {
  getQuotationAttachments,
  getQuotationById,
  getQuotationCustomers,
  getQuotationFollowups,
  getQuotationItems,
  getQuotationRevisions,
  getQuotations,
  getQuotationTopics
} from './service';
import type { QuotationFilters } from './types';

export const quotationKeys = {
  all: ['crm-quotations'] as const,
  lists: () => [...quotationKeys.all, 'list'] as const,
  list: (filters: QuotationFilters) => [...quotationKeys.lists(), filters] as const,
  details: () => [...quotationKeys.all, 'detail'] as const,
  detail: (id: string) => [...quotationKeys.details(), id] as const,
  itemsRoot: () => [...quotationKeys.all, 'items'] as const,
  items: (id: string) => [...quotationKeys.itemsRoot(), id] as const,
  customersRoot: () => [...quotationKeys.all, 'customers'] as const,
  customers: (id: string) => [...quotationKeys.customersRoot(), id] as const,
  topicsRoot: () => [...quotationKeys.all, 'topics'] as const,
  topics: (id: string) => [...quotationKeys.topicsRoot(), id] as const,
  followupsRoot: () => [...quotationKeys.all, 'followups'] as const,
  followups: (id: string) => [...quotationKeys.followupsRoot(), id] as const,
  attachmentsRoot: () => [...quotationKeys.all, 'attachments'] as const,
  attachments: (id: string) => [...quotationKeys.attachmentsRoot(), id] as const,
  revisionsRoot: () => [...quotationKeys.all, 'revisions'] as const,
  revisions: (id: string) => [...quotationKeys.revisionsRoot(), id] as const
};

export const quotationsQueryOptions = (filters: QuotationFilters) =>
  queryOptions({
    queryKey: quotationKeys.list(filters),
    queryFn: () => getQuotations(filters)
  });

export const quotationByIdOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.detail(id),
    queryFn: () => getQuotationById(id)
  });

export const quotationItemsOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.items(id),
    queryFn: () => getQuotationItems(id)
  });

export const quotationCustomersOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.customers(id),
    queryFn: () => getQuotationCustomers(id)
  });

export const quotationTopicsOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.topics(id),
    queryFn: () => getQuotationTopics(id)
  });

export const quotationFollowupsOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.followups(id),
    queryFn: () => getQuotationFollowups(id)
  });

export const quotationAttachmentsOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.attachments(id),
    queryFn: () => getQuotationAttachments(id)
  });

export const quotationRevisionsOptions = (id: string) =>
  queryOptions({
    queryKey: quotationKeys.revisions(id),
    queryFn: () => getQuotationRevisions(id)
  });
