import { queryOptions } from '@tanstack/react-query';
import {
  getApprovals,
  getCrmDashboardData,
  getCrmReferenceData,
  getCustomerById,
  getCustomers,
  getOpportunities,
  getOpportunityById,
  getQuotationById,
  getQuotations
} from './service';
import type { ApprovalFilters, CustomerFilters, OpportunityFilters, 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,
  opportunities: (filters: OpportunityFilters) => [...crmKeys.all, 'opportunities', filters] as const,
  opportunity: (id: string) => [...crmKeys.all, 'opportunity', 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 opportunitiesQueryOptions = (filters: OpportunityFilters) =>
  queryOptions({
    queryKey: crmKeys.opportunities(filters),
    queryFn: () => getOpportunities(filters)
  });

export const opportunityByIdOptions = (id: string) =>
  queryOptions({
    queryKey: crmKeys.opportunity(id),
    queryFn: () => getOpportunityById(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)
  });

