/**
 * CEO Dashboard API 연동 Hook
 *
 * 각 섹션별 API 호출 및 데이터 변환 담당
 * 제네릭 useDashboardFetch 훅을 활용하여 보일러플레이트 최소화
 */

import { useState, useCallback, useEffect, useMemo } from 'react';

import { useDashboardFetch } from './useDashboardFetch';

import type {
  DailyReportApiResponse,
  ReceivablesApiResponse,
  BadDebtApiResponse,
  ExpectedExpenseApiResponse,
  CardTransactionApiResponse,
  StatusBoardApiResponse,
  TodayIssueApiResponse,
  CalendarApiResponse,
  VatApiResponse,
  VatDetailApiResponse,
  EntertainmentApiResponse,
  EntertainmentDetailApiResponse,
  WelfareApiResponse,
  WelfareDetailApiResponse,
  ExpectedExpenseDashboardDetailApiResponse,
  SalesStatusApiResponse,
  PurchaseStatusApiResponse,
  DailyProductionApiResponse,
  UnshippedApiResponse,
  ConstructionApiResponse,
  DailyAttendanceApiResponse,
  LoanDashboardApiResponse,
  TaxSimulationApiResponse,
} from '@/lib/api/dashboard/types';

import {
  transformDailyReportResponse,
  transformReceivableResponse,
  transformDebtCollectionResponse,
  transformMonthlyExpenseResponse,
  transformCardManagementResponse,
  transformStatusBoardResponse,
  transformTodayIssueResponse,
  transformCalendarResponse,
  transformVatResponse,
  transformVatDetailResponse,
  transformEntertainmentResponse,
  transformEntertainmentDetailResponse,
  transformWelfareResponse,
  transformWelfareDetailResponse,
  transformExpectedExpenseDetailResponse,
  transformSalesStatusResponse,
  transformPurchaseStatusResponse,
  transformDailyProductionResponse,
  transformUnshippedResponse,
  transformConstructionResponse,
  transformDailyAttendanceResponse,
} from '@/lib/api/dashboard/transformers';

import type { DashboardSectionKey } from '@/lib/dashboard-invalidation';

import type {
  DailyReportData,
  ReceivableData,
  DebtCollectionData,
  MonthlyExpenseData,
  CardManagementData,
  TodayIssueItem,
  TodayIssueListItem,
  CalendarScheduleItem,
  VatData,
  EntertainmentData,
  WelfareData,
  DetailModalConfig,
  SalesStatusData,
  PurchaseStatusData,
  DailyProductionData,
  UnshippedData,
  ConstructionData,
  DailyAttendanceData,
} from '@/components/business/CEODashboard/types';

// ============================================
// 쿼리 파라미터 빌더 유틸리티
// ============================================

function buildEndpoint(
  base: string,
  params: Record<string, string | number | boolean | null | undefined>,
): string {
  const searchParams = new URLSearchParams();
  for (const [key, value] of Object.entries(params)) {
    if (value != null && value !== '') {
      searchParams.append(key, String(value));
    }
  }
  const qs = searchParams.toString();
  return qs ? `${base}?${qs}` : base;
}

// ============================================
// CardManagement 전용 fetch 유틸리티
// ============================================

async function fetchProxyJson<T>(path: string): Promise<{ success: boolean; data: T | null }> {
  try {
    const r = await fetch(`/api/proxy${path}`);
    if (!r.ok) return { success: false, data: null };
    const json = await r.json();
    return { success: !!json.success, data: json.data ?? null };
  } catch {
    return { success: false, data: null };
  }
}

async function fetchCardManagementData(fallbackData?: CardManagementData) {
  const currentYear = new Date().getFullYear();
  const [cardApiData, loanResponse, taxResponse] = await Promise.all([
    fetch('/api/proxy/card-transactions/summary').then(async (r) => {
      if (!r.ok) throw new Error(`API 오류: ${r.status}`);
      const json = await r.json();
      if (!json.success) throw new Error(json.message || '데이터 조회 실패');
      return json.data as CardTransactionApiResponse;
    }),
    fetchProxyJson<LoanDashboardApiResponse>('/loans/dashboard'),
    fetchProxyJson<TaxSimulationApiResponse>(`/loans/tax-simulation?year=${currentYear}`),
  ]);

  const loanData = loanResponse.success ? loanResponse.data : null;
  const taxData = taxResponse.success ? taxResponse.data : null;

  return transformCardManagementResponse(cardApiData, loanData, taxData, fallbackData);
}

// ============================================
// 당월 날짜 범위 유틸리티
// ============================================

function getCurrentMonthEndpoint(base: string): string {
  const now = new Date();
  const y = now.getFullYear();
  const m = now.getMonth() + 1;
  const startDate = `${y}-${String(m).padStart(2, '0')}-01`;
  const lastDay = new Date(y, m, 0).getDate();
  const endDate = `${y}-${String(m).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`;
  return buildEndpoint(base, { start_date: startDate, end_date: endDate });
}

// ============================================
// 1~4. 단순 섹션 Hooks (파라미터 없음)
// ============================================

export function useDailyReport() {
  return useDashboardFetch<DailyReportApiResponse, DailyReportData>(
    'daily-report/summary',
    transformDailyReportResponse,
  );
}

export function useReceivable() {
  return useDashboardFetch<ReceivablesApiResponse, ReceivableData>(
    'receivables/summary',
    transformReceivableResponse,
  );
}

export function useDebtCollection() {
  return useDashboardFetch<BadDebtApiResponse, DebtCollectionData>(
    'bad-debts/summary',
    transformDebtCollectionResponse,
  );
}

export function useMonthlyExpense() {
  return useDashboardFetch<ExpectedExpenseApiResponse, MonthlyExpenseData>(
    getCurrentMonthEndpoint('expected-expenses/summary'),
    transformMonthlyExpenseResponse,
  );
}

// ============================================
// 5. CardManagement Hook (커스텀: 3개 API 병렬 호출)
// ============================================

export function useCardManagement(fallbackData?: CardManagementData) {
  const [data, setData] = useState<CardManagementData | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const fetchData = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const result = await fetchCardManagementData(fallbackData);
      setData(result);
    } catch (err) {
      setError(err instanceof Error ? err.message : '데이터 로딩 실패');
      console.error('CardManagement API Error:', err);
    } finally {
      setLoading(false);
    }
  }, [fallbackData]);

  useEffect(() => {
    fetchData();
  }, [fetchData]);

  return { data, loading, error, refetch: fetchData };
}

// ============================================
// 6. StatusBoard Hook
// ============================================

export function useStatusBoard() {
  return useDashboardFetch<StatusBoardApiResponse, TodayIssueItem[]>(
    'status-board/summary',
    transformStatusBoardResponse,
  );
}

// ============================================
// 7. TodayIssue Hook
// ============================================

export interface TodayIssueData {
  items: TodayIssueListItem[];
  totalCount: number;
}

export function useTodayIssue(limit: number = 30) {
  const endpoint = useMemo(
    () => buildEndpoint('today-issues/summary', { limit }),
    [limit],
  );
  return useDashboardFetch<TodayIssueApiResponse, TodayIssueData>(
    endpoint,
    transformTodayIssueResponse,
  );
}

// ============================================
// 7-1. PastIssue Hook (이전 이슈 - 날짜별 조회)
// ============================================

export function usePastIssue(date: string | null, limit: number = 30) {
  const endpoint = useMemo(
    () => (date ? buildEndpoint('today-issues/summary', { limit, date }) : null),
    [date, limit],
  );
  return useDashboardFetch<TodayIssueApiResponse, TodayIssueData>(
    endpoint,
    transformTodayIssueResponse,
    { initialLoading: false },
  );
}

// ============================================
// 8. Calendar Hook
// ============================================

export interface CalendarData {
  items: CalendarScheduleItem[];
  totalCount: number;
}

export interface UseCalendarOptions {
  startDate?: string;
  endDate?: string;
  type?: 'schedule' | 'order' | 'construction' | 'other' | null;
  departmentFilter?: 'all' | 'department' | 'personal';
}

export function useCalendar(options: UseCalendarOptions = {}) {
  const { startDate, endDate, type, departmentFilter = 'all' } = options;

  const endpoint = useMemo(
    () =>
      buildEndpoint('calendar/schedules', {
        start_date: startDate,
        end_date: endDate,
        type: type ?? undefined,
        department_filter: departmentFilter,
      }),
    [startDate, endDate, type, departmentFilter],
  );

  return useDashboardFetch<CalendarApiResponse, CalendarData>(
    endpoint,
    transformCalendarResponse,
  );
}

// ============================================
// 9. Vat Hook
// ============================================

export interface UseVatOptions {
  periodType?: 'quarter' | 'half' | 'year';
  year?: number;
  period?: number;
}

export function useVat(options: UseVatOptions = {}) {
  const { periodType = 'quarter', year, period } = options;

  const endpoint = useMemo(
    () =>
      buildEndpoint('vat/summary', {
        period_type: periodType,
        year,
        period,
      }),
    [periodType, year, period],
  );

  return useDashboardFetch<VatApiResponse, VatData>(endpoint, transformVatResponse);
}

// ============================================
// 9-1. VatDetail Hook (부가세 상세 - 모달용)
// ============================================

export function useVatDetail() {
  const endpoint = useMemo(() => 'vat/detail', []);

  const result = useDashboardFetch<VatDetailApiResponse, DetailModalConfig>(
    endpoint,
    transformVatDetailResponse,
    { lazy: true },
  );

  return {
    modalConfig: result.data,
    loading: result.loading,
    error: result.error,
    refetch: result.refetch,
  };
}

// ============================================
// 10. Entertainment Hook (접대비)
// ============================================

export interface UseEntertainmentOptions {
  limitType?: 'annual' | 'quarterly';
  companyType?: 'large' | 'medium' | 'small';
  year?: number;
  quarter?: number;
}

export function useEntertainment(options: UseEntertainmentOptions = {}) {
  const { limitType = 'quarterly', companyType = 'medium', year, quarter } = options;

  const endpoint = useMemo(
    () =>
      buildEndpoint('entertainment/summary', {
        limit_type: limitType,
        company_type: companyType,
        year,
        quarter,
      }),
    [limitType, companyType, year, quarter],
  );

  return useDashboardFetch<EntertainmentApiResponse, EntertainmentData>(
    endpoint,
    transformEntertainmentResponse,
  );
}

// ============================================
// 11. Welfare Hook (복리후생비)
// ============================================

export interface UseWelfareOptions {
  limitType?: 'annual' | 'quarterly';
  calculationType?: 'fixed' | 'ratio';
  fixedAmountPerMonth?: number;
  ratio?: number;
  year?: number;
  quarter?: number;
}

export function useWelfare(options: UseWelfareOptions = {}) {
  const {
    limitType = 'quarterly',
    calculationType = 'fixed',
    fixedAmountPerMonth,
    ratio,
    year,
    quarter,
  } = options;

  const endpoint = useMemo(
    () =>
      buildEndpoint('welfare/summary', {
        limit_type: limitType,
        calculation_type: calculationType,
        fixed_amount_per_month: fixedAmountPerMonth,
        ratio,
        year,
        quarter,
      }),
    [limitType, calculationType, fixedAmountPerMonth, ratio, year, quarter],
  );

  return useDashboardFetch<WelfareApiResponse, WelfareData>(
    endpoint,
    transformWelfareResponse,
  );
}

// ============================================
// 11-1. EntertainmentDetail Hook (접대비 상세 - 모달용)
// ============================================

export interface UseEntertainmentDetailOptions {
  companyType?: 'large' | 'medium' | 'small';
  year?: number;
  quarter?: number;
}

export function useEntertainmentDetail(options: UseEntertainmentDetailOptions = {}) {
  const { companyType = 'medium', year, quarter } = options;

  const endpoint = useMemo(
    () =>
      buildEndpoint('entertainment/detail', {
        company_type: companyType,
        year,
        quarter,
      }),
    [companyType, year, quarter],
  );

  const result = useDashboardFetch<EntertainmentDetailApiResponse, DetailModalConfig>(
    endpoint,
    transformEntertainmentDetailResponse,
    { lazy: true },
  );

  return {
    modalConfig: result.data,
    loading: result.loading,
    error: result.error,
    refetch: result.refetch,
  };
}

// ============================================
// 12. WelfareDetail Hook (복리후생비 상세 - 모달용)
// ============================================

export interface UseWelfareDetailOptions {
  calculationType?: 'fixed' | 'ratio';
  fixedAmountPerMonth?: number;
  ratio?: number;
  year?: number;
  quarter?: number;
}

export function useWelfareDetail(options: UseWelfareDetailOptions = {}) {
  const {
    calculationType = 'fixed',
    fixedAmountPerMonth,
    ratio,
    year,
    quarter,
  } = options;

  const endpoint = useMemo(
    () =>
      buildEndpoint('welfare/detail', {
        calculation_type: calculationType,
        fixed_amount_per_month: fixedAmountPerMonth,
        ratio,
        year,
        quarter,
      }),
    [calculationType, fixedAmountPerMonth, ratio, year, quarter],
  );

  const result = useDashboardFetch<WelfareDetailApiResponse, DetailModalConfig>(
    endpoint,
    transformWelfareDetailResponse,
    { lazy: true },
  );

  return {
    modalConfig: result.data,
    loading: result.loading,
    error: result.error,
    refetch: result.refetch,
  };
}

// ============================================
// 13. SalesStatus Hook (매출 현황)
// ============================================

export function useSalesStatus() {
  return useDashboardFetch<SalesStatusApiResponse, SalesStatusData>(
    'dashboard/sales/summary',
    transformSalesStatusResponse,
  );
}

// ============================================
// 14. PurchaseStatus Hook (매입 현황)
// ============================================

export function usePurchaseStatus() {
  return useDashboardFetch<PurchaseStatusApiResponse, PurchaseStatusData>(
    'dashboard/purchases/summary',
    transformPurchaseStatusResponse,
  );
}

// ============================================
// 15. DailyProduction Hook (생산 현황)
// ============================================

export function useDailyProduction() {
  return useDashboardFetch<DailyProductionApiResponse, DailyProductionData>(
    'dashboard/production/summary',
    transformDailyProductionResponse,
  );
}

// ============================================
// 16. Unshipped Hook (미출고 내역)
// ============================================

export function useUnshipped() {
  return useDashboardFetch<UnshippedApiResponse, UnshippedData>(
    'dashboard/unshipped/summary',
    transformUnshippedResponse,
  );
}

// ============================================
// 17. Construction Hook (시공 현황)
// ============================================

export function useConstruction() {
  return useDashboardFetch<ConstructionApiResponse, ConstructionData>(
    'dashboard/construction/summary',
    transformConstructionResponse,
  );
}

// ============================================
// 18. DailyAttendance Hook (근태 현황)
// ============================================

export function useDailyAttendance() {
  return useDashboardFetch<DailyAttendanceApiResponse, DailyAttendanceData>(
    'dashboard/attendance/summary',
    transformDailyAttendanceResponse,
  );
}

// ============================================
// 19. MonthlyExpenseDetail Hook (당월 예상 지출 상세 - 통합 모달용)
// ============================================

export type MonthlyExpenseCardId = 'me1' | 'me2' | 'me3' | 'me4';

export function useMonthlyExpenseDetail() {
  const [modalConfig, setModalConfig] = useState<DetailModalConfig | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // cardId → transaction_type 매핑
  const CARD_TRANSACTION_TYPE: Record<MonthlyExpenseCardId, string | undefined> = {
    me1: 'purchase',
    me2: 'card',
    me3: 'bill',
    me4: undefined, // 전체
  };

  const fetchData = useCallback(async (cardId: MonthlyExpenseCardId, filterParams?: { startDate?: string; endDate?: string; search?: string }) => {
    try {
      setLoading(true);
      setError(null);

      // 대시보드 전용 API 엔드포인트 구성
      const transactionType = CARD_TRANSACTION_TYPE[cardId];
      const params: Record<string, string | undefined> = {
        transaction_type: transactionType,
        start_date: filterParams?.startDate,
        end_date: filterParams?.endDate,
        search: filterParams?.search,
      };
      const endpoint = buildEndpoint('/api/proxy/expected-expenses/dashboard-detail', params);

      const response = await fetch(endpoint);
      if (!response.ok) {
        throw new Error(`API 오류: ${response.status}`);
      }
      const result = await response.json();
      if (!result.success) {
        throw new Error(result.message || '데이터 조회 실패');
      }

      const transformed = transformExpectedExpenseDetailResponse(
        result.data as ExpectedExpenseDashboardDetailApiResponse,
        cardId,
      );

      setModalConfig(transformed);
      return transformed;
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '데이터 로딩 실패';
      setError(errorMessage);
      console.error('MonthlyExpenseDetail API Error:', err);
      return null;
    } finally {
      setLoading(false);
    }
  }, []);

  return { modalConfig, loading, error, fetchData };
}

// ============================================
// 통합 Dashboard Hook
// ============================================

export interface UseCEODashboardOptions {
  dailyReport?: boolean;
  receivable?: boolean;
  debtCollection?: boolean;
  monthlyExpense?: boolean;
  cardManagement?: boolean;
  cardManagementFallback?: CardManagementData;
  statusBoard?: boolean;
  salesStatus?: boolean;
  purchaseStatus?: boolean;
  dailyProduction?: boolean;
  unshipped?: boolean;
  construction?: boolean;
  dailyAttendance?: boolean;
}

interface SectionState<T> {
  data: T | null;
  loading: boolean;
  error: string | null;
}

export interface CEODashboardState {
  dailyReport: SectionState<DailyReportData>;
  receivable: SectionState<ReceivableData>;
  debtCollection: SectionState<DebtCollectionData>;
  monthlyExpense: SectionState<MonthlyExpenseData>;
  cardManagement: SectionState<CardManagementData>;
  statusBoard: SectionState<TodayIssueItem[]>;
  salesStatus: SectionState<SalesStatusData>;
  purchaseStatus: SectionState<PurchaseStatusData>;
  dailyProduction: SectionState<DailyProductionData>;
  unshipped: SectionState<UnshippedData>;
  construction: SectionState<ConstructionData>;
  dailyAttendance: SectionState<DailyAttendanceData>;
  refetchAll: () => void;
  refetchMap: Partial<Record<DashboardSectionKey, () => void>>;
}

export function useCEODashboard(options: UseCEODashboardOptions = {}): CEODashboardState {
  const {
    dailyReport: enableDailyReport = true,
    receivable: enableReceivable = true,
    debtCollection: enableDebtCollection = true,
    monthlyExpense: enableMonthlyExpense = true,
    cardManagement: enableCardManagement = true,
    cardManagementFallback,
    statusBoard: enableStatusBoard = true,
    salesStatus: enableSalesStatus = true,
    purchaseStatus: enablePurchaseStatus = true,
    dailyProduction: enableDailyProduction = true,
    unshipped: enableUnshipped = true,
    construction: enableConstruction = true,
    dailyAttendance: enableDailyAttendance = true,
  } = options;

  // 비활성 섹션은 endpoint를 null로 → useDashboardFetch가 skip
  const dr = useDashboardFetch<DailyReportApiResponse, DailyReportData>(
    enableDailyReport ? 'daily-report/summary' : null,
    transformDailyReportResponse,
    { initialLoading: enableDailyReport },
  );
  const rv = useDashboardFetch<ReceivablesApiResponse, ReceivableData>(
    enableReceivable ? 'receivables/summary' : null,
    transformReceivableResponse,
    { initialLoading: enableReceivable },
  );
  const dc = useDashboardFetch<BadDebtApiResponse, DebtCollectionData>(
    enableDebtCollection ? 'bad-debts/summary' : null,
    transformDebtCollectionResponse,
    { initialLoading: enableDebtCollection },
  );
  const sb = useDashboardFetch<StatusBoardApiResponse, TodayIssueItem[]>(
    enableStatusBoard ? 'status-board/summary' : null,
    transformStatusBoardResponse,
    { initialLoading: enableStatusBoard },
  );
  const ss = useDashboardFetch<SalesStatusApiResponse, SalesStatusData>(
    enableSalesStatus ? 'dashboard/sales/summary' : null,
    transformSalesStatusResponse,
    { initialLoading: enableSalesStatus },
  );
  const ps = useDashboardFetch<PurchaseStatusApiResponse, PurchaseStatusData>(
    enablePurchaseStatus ? 'dashboard/purchases/summary' : null,
    transformPurchaseStatusResponse,
    { initialLoading: enablePurchaseStatus },
  );
  const dp = useDashboardFetch<DailyProductionApiResponse, DailyProductionData>(
    enableDailyProduction ? 'dashboard/production/summary' : null,
    transformDailyProductionResponse,
    { initialLoading: enableDailyProduction },
  );
  const us = useDashboardFetch<UnshippedApiResponse, UnshippedData>(
    enableUnshipped ? 'dashboard/unshipped/summary' : null,
    transformUnshippedResponse,
    { initialLoading: enableUnshipped },
  );
  const cs = useDashboardFetch<ConstructionApiResponse, ConstructionData>(
    enableConstruction ? 'dashboard/construction/summary' : null,
    transformConstructionResponse,
    { initialLoading: enableConstruction },
  );
  const da = useDashboardFetch<DailyAttendanceApiResponse, DailyAttendanceData>(
    enableDailyAttendance ? 'dashboard/attendance/summary' : null,
    transformDailyAttendanceResponse,
    { initialLoading: enableDailyAttendance },
  );

  // MonthlyExpense: 대시보드 전용 API (당월 필터)
  const me = useDashboardFetch<ExpectedExpenseApiResponse, MonthlyExpenseData>(
    enableMonthlyExpense ? getCurrentMonthEndpoint('expected-expenses/summary') : null,
    transformMonthlyExpenseResponse,
    { initialLoading: enableMonthlyExpense },
  );

  // CardManagement: 커스텀 (3개 API 병렬)
  const [cmData, setCmData] = useState<CardManagementData | null>(null);
  const [cmLoading, setCmLoading] = useState(enableCardManagement);
  const [cmError, setCmError] = useState<string | null>(null);

  const fetchCM = useCallback(async () => {
    if (!enableCardManagement) return;
    try {
      setCmLoading(true);
      setCmError(null);
      const result = await fetchCardManagementData(cardManagementFallback);
      setCmData(result);
    } catch (err) {
      setCmError(err instanceof Error ? err.message : '데이터 로딩 실패');
      console.error('CardManagement API Error:', err);
    } finally {
      setCmLoading(false);
    }
  }, [enableCardManagement, cardManagementFallback]);

  useEffect(() => {
    fetchCM();
  }, [fetchCM]);

  const refetchAll = useCallback(() => {
    dr.refetch();
    rv.refetch();
    dc.refetch();
    me.refetch();
    fetchCM();
    sb.refetch();
    ss.refetch();
    ps.refetch();
    dp.refetch();
    us.refetch();
    cs.refetch();
    da.refetch();
     
  }, [dr.refetch, rv.refetch, dc.refetch, me.refetch, fetchCM, sb.refetch, ss.refetch, ps.refetch, dp.refetch, us.refetch, cs.refetch, da.refetch]);

  // 섹션별 refetch 함수 맵 (targeted invalidation용)
  const refetchMap = useMemo<Partial<Record<DashboardSectionKey, () => void>>>(() => ({
    dailyReport: dr.refetch,
    receivable: rv.refetch,
    debtCollection: dc.refetch,
    monthlyExpense: me.refetch,
    cardManagement: fetchCM,
    statusBoard: sb.refetch,
    salesStatus: ss.refetch,
    purchaseStatus: ps.refetch,
    dailyProduction: dp.refetch,
    unshipped: us.refetch,
    construction: cs.refetch,
    dailyAttendance: da.refetch,
  }), [dr.refetch, rv.refetch, dc.refetch, me.refetch, fetchCM, sb.refetch, ss.refetch, ps.refetch, dp.refetch, us.refetch, cs.refetch, da.refetch]);

  return {
    dailyReport: { data: dr.data, loading: dr.loading, error: dr.error },
    receivable: { data: rv.data, loading: rv.loading, error: rv.error },
    debtCollection: { data: dc.data, loading: dc.loading, error: dc.error },
    monthlyExpense: { data: me.data, loading: me.loading, error: me.error },
    cardManagement: { data: cmData, loading: cmLoading, error: cmError },
    statusBoard: { data: sb.data, loading: sb.loading, error: sb.error },
    salesStatus: { data: ss.data, loading: ss.loading, error: ss.error },
    purchaseStatus: { data: ps.data, loading: ps.loading, error: ps.error },
    dailyProduction: { data: dp.data, loading: dp.loading, error: dp.error },
    unshipped: { data: us.data, loading: us.loading, error: us.error },
    construction: { data: cs.data, loading: cs.loading, error: cs.error },
    dailyAttendance: { data: da.data, loading: da.loading, error: da.error },
    refetchAll,
    refetchMap,
  };
}