import { useUserRecords } from "./useUserRecords";
import { useExternalIncomingRecords } from "./useExternalIncomingRecords";
import { useInternalIncomingRecords } from "./useInternalIncomingRecords";
import { useCCIncomingRecords } from "./useCCIncomingRecords";
import { useApprovalTasks, useMyApprovals } from "./useApprovalTasks";
import { usePendingRecords } from "./usePendingRecords";

export interface NotificationProps {
  incomingCount: number;
  approvalCount: number;
  internalIncomingCount: number;
  externalIncomingCount: number;
  ccIncomingCount: number;
  pendingCount: number;
}

const useCounter = () => {
  const { recordsResponse, isError, isLoading, refetch } = useUserRecords();
  // Grouped counters for outgoing statuses

  const { recordsResponse: draftOutgoing } = useUserRecords({
    status: "Draft",
  });
  const { recordsResponse: submittedOutgoing } = useUserRecords({
    status: "Submitted",
  });
  const { recordsResponse: acceptedOutgoing } = useUserRecords({
    status: "Accepted",
  });
  const { recordsResponse: approvedOutgoing } = useUserRecords({
    status: "Approved",
  });
  const { recordsResponse: rejectedOutgoing } = useUserRecords({
    status: "Rejected",
  });
  const { recordsResponse: returnedOutgoing } = useUserRecords({
    status: "Returned",
  });
  const { recordsResponse: adjustmentOutgoing } = useUserRecords({
    status: "Adjustment",
  });
  const { recordsResponse: sentOutgoing } = useUserRecords({
    status: "Dispatched",
  });
  const outgoingCounters = {
    Draft: draftOutgoing?.count,
    Accepted: acceptedOutgoing?.count,
    Rejected: rejectedOutgoing?.count,
    Adjustment: adjustmentOutgoing?.count,
    Dispatched: sentOutgoing?.count,
    Submitted: submittedOutgoing?.count,
    Approved: approvedOutgoing?.count,
    Returned: returnedOutgoing?.count,
  };
  const totalOutgoingRecords = recordsResponse?.count;

  // External incoming counts
  const { externalCount: draftCount } = useExternalIncomingRecords({
    status: "draft",
  });

  const { externalCount: acceptedCount } = useExternalIncomingRecords({
    status: "accepted",
  });
  const { externalCount: rejectdCount } = useExternalIncomingRecords({
    status: "rejected",
  });
  const { externalCount: reassignCount } = useExternalIncomingRecords({
    status: "re_assigned",
  });

  const externalCountdraft = draftCount ?? 0;
  const externalCountAccepted = acceptedCount ?? 0;
  const externalCountRejected = rejectdCount ?? 0;
  const externalCountReassigned = reassignCount ?? 0;
  const TotalExternal =
    externalCountdraft +
    externalCountAccepted +
    externalCountRejected +
    externalCountReassigned;

  // Internal incoming counts
  const { InternalCount: draftCountI } = useInternalIncomingRecords({
    status: "draft",
  });
  const { InternalCount: acceptedCountI } = useInternalIncomingRecords({
    status: "accepted",
  });
  const { InternalCount: rejectdCountI } = useInternalIncomingRecords({
    status: "rejected",
  });
  const { InternalCount: reassignCountI } = useInternalIncomingRecords({
    status: "re_assigned",
  });

  const internalCountdraft = draftCountI ?? 0;
  const internalCountAccepted = acceptedCountI ?? 0;
  const internalCountRejected = rejectdCountI ?? 0;
  const internalCountReassigned = reassignCountI ?? 0;
  const TotalInternal =
    internalCountdraft +
    internalCountAccepted +
    internalCountRejected +
    internalCountReassigned;

  // CC counts
  const { ccCount: allcc } = useCCIncomingRecords();

  const TotalCC = allcc ?? 0;
  // Approval counts grouped
  const { approvalCount: appoveCount } = useApprovalTasks();
  const { data: approvalApprove } = useMyApprovals({ action: "send_to_ro" });
  const { data: acceptApprove } = useMyApprovals({ action: "approve" });
  const { data: adjustmentApprove } = useMyApprovals({ action: "send_back" });
  const { data: rejectedApprove } = useMyApprovals({ action: "reject" });
  const approvalData1 = appoveCount ?? 0;
  const { pendingRecordsCount } = usePendingRecords();
  const approvalCounters = {
    active: approvalData1,
    accepted: acceptApprove?.count ?? 0,
    rejected: rejectedApprove?.count ?? 0,
    adjustment: adjustmentApprove?.count ?? 0,
    approved: approvalApprove?.count ?? 0,
  };
  //const TotalApproval = approvalData1;
  const TotalApproval = Object.values(approvalCounters).reduce(
    (sum, val) => sum + (val ?? 0),
    0,
  );
  // Combined totals for incoming records by status
  const TotalIncomingRecordsDrafts = externalCountdraft + internalCountdraft;
  const TotalIncomingRecordsAccepted =
    externalCountAccepted + internalCountAccepted;
  const TotalIncomingRecordsRejected =
    externalCountRejected + internalCountRejected;
  const TotalIncomingRecordsReassigned =
    externalCountReassigned + internalCountReassigned;
  const incomingCount =
    TotalIncomingRecordsDrafts +
    TotalIncomingRecordsAccepted +
    TotalIncomingRecordsRejected +
    TotalIncomingRecordsReassigned;

  return {
    isLoading,
    isError,
    refetch,
    tot: totalOutgoingRecords,
    externalIncomingCount: TotalExternal,
    internalIncomingCount: TotalInternal,
    ccIncomingCount: TotalCC,
    approvalCount: TotalApproval,
    externalCountReassigned,
    externalCountRejected,
    externalCountAccepted,
    draftCount,
    internalCountReassigned,
    internalCountRejected,
    internalCountAccepted,
    internalCountdraft,
    TotalIncomingRecordsDrafts,
    TotalIncomingRecordsAccepted,
    TotalIncomingRecordsReassigned,
    TotalIncomingRecordsRejected,
    outgoingCounters,
    approvalCounters,
    incomingCount,
    pendingCount: pendingRecordsCount,
    TotalExternal,
    TotalInternal,
    TotalCC,
    TotalApproval,
  };
};

export default useCounter;
