/**
 * Hook for managing dashboard pagination state and handlers
 */

import { useState, useCallback } from 'react';

interface PaginationState {
  page: number;
  totalPages: number;
  totalRecords: number;
}

export function useDashboardPagination() {
  const [activity, setActivity] = useState<PaginationState>({
    page: 1,
    totalPages: 1,
    totalRecords: 0,
  });
  
  const [critical, setCritical] = useState<PaginationState>({
    page: 1,
    totalPages: 1,
    totalRecords: 0,
  });
  
  const [deadlines, setDeadlines] = useState<PaginationState>({
    page: 1,
    totalPages: 1,
    totalRecords: 0,
  });
  
  const [approver, setApprover] = useState<PaginationState>({
    page: 1,
    totalPages: 1,
    totalRecords: 0,
  });

  const updateActivityPagination = useCallback((page: number, totalPages: number, totalRecords: number) => {
    setActivity({ page, totalPages, totalRecords });
  }, []);

  const updateCriticalPagination = useCallback((page: number, totalPages: number, totalRecords: number) => {
    setCritical({ page, totalPages, totalRecords });
  }, []);

  const updateDeadlinesPagination = useCallback((page: number, totalPages: number, totalRecords: number) => {
    setDeadlines({ page, totalPages, totalRecords });
  }, []);

  const updateApproverPagination = useCallback((page: number, totalPages: number, totalRecords: number) => {
    setApprover({ page, totalPages, totalRecords });
  }, []);

  const handleActivityPageChange = useCallback((newPage: number, onPageChange: (page: number) => void) => {
    if (newPage >= 1 && newPage <= activity.totalPages) {
      setActivity(prev => ({ ...prev, page: newPage }));
      onPageChange(newPage);
    }
  }, [activity.totalPages]);

  const handleCriticalPageChange = useCallback((newPage: number, onPageChange: (page: number) => void) => {
    if (newPage >= 1 && newPage <= critical.totalPages) {
      setCritical(prev => ({ ...prev, page: newPage }));
      onPageChange(newPage);
    }
  }, [critical.totalPages]);

  const handleDeadlinesPageChange = useCallback((newPage: number, onPageChange: (page: number) => void) => {
    if (newPage >= 1 && newPage <= deadlines.totalPages) {
      setDeadlines(prev => ({ ...prev, page: newPage }));
      onPageChange(newPage);
    }
  }, [deadlines.totalPages]);

  const handleApproverPageChange = useCallback((newPage: number, onPageChange: (page: number) => void) => {
    if (newPage >= 1 && newPage <= approver.totalPages) {
      setApprover(prev => ({ ...prev, page: newPage }));
      onPageChange(newPage);
    }
  }, [approver.totalPages]);

  return {
    activity,
    critical,
    deadlines,
    approver,
    updateActivityPagination,
    updateCriticalPagination,
    updateDeadlinesPagination,
    updateApproverPagination,
    handleActivityPageChange,
    handleCriticalPageChange,
    handleDeadlinesPageChange,
    handleApproverPageChange,
  };
}

