import { queryOptions } from '@tanstack/react-query';
import {
  getApprovals,
  getCrmDashboardData,
  getCrmReferenceData,
  getCustomerById,
  getCustomers,
  getEnquiries,
  getEnquiryById,
  getQuotationById,
  getQuotations
} from './service';
import type { ApprovalFilters, CustomerFilters, EnquiryFilters, QuotationFilters } from './types';

export const crmKeys = {
  all: ['crm'] as const,
  references: () => [...crmKeys.all, 'references'] as const,
  dashboard: () => [...crmKeys.all, 'dashboard'] as const,
  customers: (filters: CustomerFilters) => [...crmKeys.all, 'customers', filters] as const,
  customer: (id: string) => [...crmKeys.all, 'customer', id] as const,
  enquiries: (filters: EnquiryFilters) => [...crmKeys.all, 'enquiries', filters] as const,
  enquiry: (id: string) => [...crmKeys.all, 'enquiry', id] as const,
  quotations: (filters: QuotationFilters) => [...crmKeys.all, 'quotations', filters] as const,
  quotation: (id: string) => [...crmKeys.all, 'quotation', id] as const,
  approvals: (filters: ApprovalFilters) => [...crmKeys.all, 'approvals', filters] as const
};

export const crmReferenceQueryOptions = () =>
  queryOptions({
    queryKey: crmKeys.references(),
    queryFn: getCrmReferenceData
  });

export const crmDashboardQueryOptions = () =>
  queryOptions({
    queryKey: crmKeys.dashboard(),
    queryFn: getCrmDashboardData
  });

export const customersQueryOptions = (filters: CustomerFilters) =>
  queryOptions({
    queryKey: crmKeys.customers(filters),
    queryFn: () => getCustomers(filters)
  });

export const customerByIdOptions = (id: string) =>
  queryOptions({
    queryKey: crmKeys.customer(id),
    queryFn: () => getCustomerById(id)
  });

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

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

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

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

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