import { useInternalIncomingRecords } from "./useInternalIncomingRecords";
import { useExternalIncomingRecords } from "./useExternalIncomingRecords";
import { useCCIncomingRecords } from "./useCCIncomingRecords";
import { useApprovalTasks } from "./useApprovalTasks";

import { useMemo } from "react";

export interface UseReportByHooksParams {
  pagination?: {
    pageIndex: number;
    pageSize: number;
  };
  isSecretary?: boolean;
  searchParams?: Record<string, any>;
}

export const useReportByHooks = ({
  pagination = { pageIndex: 0, pageSize: 10 },
  isSecretary = false,
  searchParams = {},
}: UseReportByHooksParams = {}) => {
  const useParentCounts = isSecretary;
  // Base params for all queries
  const baseParams = {
    skip: pagination.pageIndex * 1,
    take: 1,
    orderBy: "record.dispatchedDate:DESC,record.createdAt:DESC",
    status: "draft",
    fetchParent: useParentCounts,
    ...searchParams,
  };

  // Internal Incoming Records - Draft
  const {
    InternalCount2: internalDraftCountSelf = 0,
    internalParentCount: internalDraftCountParent = 0,
    isLoading: internalLoading,
  } = useInternalIncomingRecords({
      pageIndex: baseParams.skip,
      pageSize: baseParams.take,
      status: "draft",
      fetchParent: useParentCounts,
      ...searchParams,
    });
  const internalDraftCount = useParentCounts
    ? internalDraftCountParent
    : internalDraftCountSelf;

  // External Incoming Records - Draft (non_smart_user)
  const {
    externalCount2: nonSmartUserDraftCountSelf = 0,
    externalParentCount: nonSmartUserDraftCountParent = 0,
    isLoading: nonSmartUserLoading,
  } = useExternalIncomingRecords({
    pageIndex: baseParams.skip,
    pageSize: baseParams.take,
    status: "draft",
    fetchParent: useParentCounts,
    ...searchParams,
    recordType: ["non_smart_user"],
  });
  const nonSmartUserDraftCount = useParentCounts
    ? nonSmartUserDraftCountParent
    : nonSmartUserDraftCountSelf;

  // External Incoming Records - Draft (external_non_smart_user)
  const {
    externalCount2: externalNonSmartUserDraftCountSelf = 0,
    externalParentCount: externalNonSmartUserDraftCountParent = 0,
    isLoading: externalNonSmartUserLoading,
  } = useExternalIncomingRecords({
    pageIndex: baseParams.skip,
    pageSize: baseParams.take,
    status: "draft",
    fetchParent: useParentCounts,
    ...searchParams,
    recordType: ["external_non_smart_user"],
  });
  const externalNonSmartUserDraftCount = useParentCounts
    ? externalNonSmartUserDraftCountParent
    : externalNonSmartUserDraftCountSelf;

  const {
    externalCount2: externalSmartDraftCountSelf = 0,
    externalParentCount: externalSmartDraftCountParent = 0,
    isLoading: externalSmartDraftLoading,
  } = useExternalIncomingRecords({
    pageIndex: baseParams.skip,
    pageSize: baseParams.take,
    status: "draft",
    fetchParent: useParentCounts,
    ...searchParams,
    recordType: ["external"],
  });
  const externalSmartDraftCount = useParentCounts
    ? externalSmartDraftCountParent
    : externalSmartDraftCountSelf;

  // External Incoming Records - Draft (external)
  const {
    externalCount2: externalDraftCountSelf = 0,
    externalParentCount: externalDraftCountParent = 0,
    isLoading: externalLoading,
  } = useExternalIncomingRecords({
    pageIndex: baseParams.skip,
    pageSize: baseParams.take,
    status: "draft",
    fetchParent: useParentCounts,
    ...searchParams,
    recordType: ["external_non_smart_user", "non_smart_user"],
  });
  const externalDraftCount = useParentCounts
    ? externalDraftCountParent
    : externalDraftCountSelf;

  // CC Incoming Records - Unseen (equivalent to unread)
  const {
    ccCount: ccUnseenCountSelf = 0,
    ccParentCount: ccUnseenCountParent = 0,
    isLoading: ccLoading,
  } = useCCIncomingRecords({
      skip: baseParams.skip,
      take: baseParams.take,
      fetchParent: useParentCounts,
      ...searchParams,
      hasSeen: false,
    });
  const ccUnseenCount = useParentCounts
    ? ccUnseenCountParent
    : ccUnseenCountSelf;

  // For Your Reference - Draft
  const {
    ccCount: forYourRefDraftCountSelf = 0,
    ccParentCount: forYourRefDraftCountParent = 0,
    isLoading: forYourRefLoading,
  } = useCCIncomingRecords({
      skip: pagination.pageIndex * 1,
      take: 1,
      orderBy: "record.dispatchedDate:DESC,record.createdAt:DESC",
      isForYourReference: true,
      ...searchParams,
      fetchParent: useParentCounts,
      hasSeen: false,
    });
  const forYourRefDraftCount = useParentCounts
    ? forYourRefDraftCountParent
    : forYourRefDraftCountSelf;

  // Approval Tasks - Active
  const { approvalCount = 0, isLoading: approvalLoading } = useApprovalTasks({
    skip: pagination.pageIndex * 1,
    take: 1,
  });

  // Compute total counts
  const totalCounts = useMemo(
    () => ({
      internalDraftCount,
      nonSmartUserDraftCount,
      externalNonSmartUserDraftCount,
      externalDraftCount,
      externalSmartDraftCount,
      ccUnseenCount,
      forYourRefDraftCount,
      approvalCount,
      totalDraftCount:
        internalDraftCount +
        externalDraftCount +
        externalSmartDraftCount +
        forYourRefDraftCount,
      totalUnseenCount: ccUnseenCount,
      totalApprovalCount: approvalCount,
    }),
    [
      internalDraftCount,
      nonSmartUserDraftCount,
      externalNonSmartUserDraftCount,
      externalDraftCount,
      externalSmartDraftCount,
      ccUnseenCount,
      forYourRefDraftCount,
      approvalCount,
    ],
  );

  const isLoading =
    internalLoading ||
    nonSmartUserLoading ||
    externalNonSmartUserLoading ||
    externalSmartDraftLoading ||
    externalLoading ||
    ccLoading ||
    forYourRefLoading ||
    approvalLoading;

  return {
    counts: totalCounts,
    isLoading,
    breakdownCounts: {
      internal: internalDraftCount,
      nonSmartUser: nonSmartUserDraftCount,
      externalNonSmartUser: externalNonSmartUserDraftCount,
      external: externalDraftCount,
      ccUnseen: ccUnseenCount,
      forYourReference: forYourRefDraftCount,
      approval: approvalCount,
      externalSmart: externalSmartDraftCount,
    },
  };
};
