const { useState, useEffect } = React;

const useAdminState = () => {
  // Existing states
  const [user, setUser] = useState(null);
  const [activeTab, setActiveTab] = useState("users");
  const [stats, setStats] = useState({});
  const [users, setUsers] = useState([]);
  const [filteredUsers, setFilteredUsers] = useState([]);
  const [currentPage, setCurrentPage] = useState(1);
  const [totalPages, setTotalPages] = useState(1);
  const [totalRecords, setTotalRecords] = useState(0);
  const [searchTerm, setSearchTerm] = useState("");
  const [roleFilter, setRoleFilter] = useState("");
  const [statusFilter, setStatusFilter] = useState("");
  const [selectedUser, setSelectedUser] = useState(null);
  const [showEditModal, setShowEditModal] = useState(false);
  const [toast, setToast] = useState(null);
  const [loading, setLoading] = useState(false);

  // Items states
  const [items, setItems] = useState([]);
  const [filteredItems, setFilteredItems] = useState([]);
  const [showExpired, setShowExpired] = useState(false);
  const [itemSearchTerm, setItemSearchTerm] = useState("");
  const [itemStatusFilter, setItemStatusFilter] = useState("");
  const [itemCategoryFilter, setItemCategoryFilter] = useState("");
  const [selectedItemDetail, setSelectedItemDetail] = useState(null);
  const [showItemDetailModal, setShowItemDetailModal] = useState(false);
  const [showCreateItemModal, setShowCreateItemModal] = useState(false);
  const [showEditItemModal, setShowEditItemModal] = useState(false);
  const [selectedItemToEdit, setSelectedItemToEdit] = useState(null);
  const [photoPreview, setPhotoPreview] = useState(null);
  const [itemPage, setItemPage] = useState(1);
  const [itemTotalPages, setItemTotalPages] = useState(1);
  const [itemTotalRecords, setItemTotalRecords] = useState(0);

  // NEW: Lost Items states
  const [lostItems, setLostItems] = useState([]);
  const [filteredLostItems, setFilteredLostItems] = useState([]);
  const [lostItemSearchTerm, setLostItemSearchTerm] = useState("");
  const [lostItemStatusFilter, setLostItemStatusFilter] = useState("");
  const [lostItemCategoryFilter, setLostItemCategoryFilter] = useState("");
  const [selectedLostItemDetail, setSelectedLostItemDetail] = useState(null);
  const [showLostItemDetailModal, setShowLostItemDetailModal] = useState(false);
  const [showCreateLostItemModal, setShowCreateLostItemModal] = useState(false);
  const [showEditLostItemModal, setShowEditLostItemModal] = useState(false);
  const [selectedLostItemToEdit, setSelectedLostItemToEdit] = useState(null);
  const [lostItemPage, setLostItemPage] = useState(1);
  const [lostItemTotalPages, setLostItemTotalPages] = useState(1);
  const [lostItemTotalRecords, setLostItemTotalRecords] = useState(0);

  // Claims states
  const [claims, setClaims] = useState([]);
  const [filteredClaims, setFilteredClaims] = useState([]);
  const [claimStatusFilter, setClaimStatusFilter] = useState("");
  const [claimSearchTerm, setClaimSearchTerm] = useState("");
  const [selectedClaim, setSelectedClaim] = useState(null);
  const [showClaimDetailModal, setShowClaimDetailModal] = useState(false);
  const [showCreateClaimModal, setShowCreateClaimModal] = useState(false);
  const [showEditClaimModal, setShowEditClaimModal] = useState(false);
  const [selectedClaimToEdit, setSelectedClaimToEdit] = useState(null);

  // Archives states
  const [archives, setArchives] = useState([]);
  const [filteredArchives, setFilteredArchives] = useState([]);
  const [archiveSearchTerm, setArchiveSearchTerm] = useState("");
  const [archiveCategoryFilter, setArchiveCategoryFilter] = useState("");
  const [selectedArchive, setSelectedArchive] = useState(null);
  const [showArchiveDetailModal, setShowArchiveDetailModal] = useState(false);

  // Audit logs states
  const [auditLogs, setAuditLogs] = useState([]);
  const [filteredAuditLogs, setFilteredAuditLogs] = useState([]);
  const [auditLogSearchTerm, setAuditLogSearchTerm] = useState("");
  const [auditLogActionFilter, setAuditLogActionFilter] = useState("");
  const [auditLogUserFilter, setAuditLogUserFilter] = useState("");
  const [auditLogDateFilter, setAuditLogDateFilter] = useState("");

  // Categories states
  const [categories, setCategories] = useState([]);
  const [showCreateCategoryModal, setShowCreateCategoryModal] = useState(false);
  const [showEditCategoryModal, setShowEditCategoryModal] = useState(false);
  const [selectedCategory, setSelectedCategory] = useState(null);

  // Roles states
  const [roles, setRoles] = useState([]);
  const [permissions, setPermissions] = useState([]);
  const [showCreateRoleModal, setShowCreateRoleModal] = useState(false);
  const [showEditRoleModal, setShowEditRoleModal] = useState(false);
  const [selectedRole, setSelectedRole] = useState(null);

  return {
    user,
    setUser,
    activeTab,
    setActiveTab,
    stats,
    setStats,
    users,
    setUsers,
    filteredUsers,
    setFilteredUsers,
    currentPage,
    setCurrentPage,
    totalPages,
    setTotalPages,
    totalRecords,
    setTotalRecords,
    searchTerm,
    setSearchTerm,
    roleFilter,
    setRoleFilter,
    statusFilter,
    setStatusFilter,
    selectedUser,
    setSelectedUser,
    showEditModal,
    setShowEditModal,
    toast,
    setToast,
    loading,
    setLoading,
    items,
    setItems,
    filteredItems,
    setFilteredItems,
    showExpired,
    setShowExpired,
    itemSearchTerm,
    setItemSearchTerm,
    itemStatusFilter,
    setItemStatusFilter,
    itemCategoryFilter,
    setItemCategoryFilter,
    selectedItemDetail,
    setSelectedItemDetail,
    showItemDetailModal,
    setShowItemDetailModal,
    showCreateItemModal,
    setShowCreateItemModal,
    showEditItemModal,
    setShowEditItemModal,
    selectedItemToEdit,
    setSelectedItemToEdit,
    photoPreview,
    setPhotoPreview,
    itemPage,
    setItemPage,
    itemTotalPages,
    setItemTotalPages,
    itemTotalRecords,
    setItemTotalRecords,
    // Lost Items
    lostItems,
    setLostItems,
    filteredLostItems,
    setFilteredLostItems,
    lostItemSearchTerm,
    setLostItemSearchTerm,
    lostItemStatusFilter,
    setLostItemStatusFilter,
    lostItemCategoryFilter,
    setLostItemCategoryFilter,
    selectedLostItemDetail,
    setSelectedLostItemDetail,
    showLostItemDetailModal,
    setShowLostItemDetailModal,
    showCreateLostItemModal,
    setShowCreateLostItemModal,
    showEditLostItemModal,
    setShowEditLostItemModal,
    selectedLostItemToEdit,
    setSelectedLostItemToEdit,
    lostItemPage,
    setLostItemPage,
    lostItemTotalPages,
    setLostItemTotalPages,
    lostItemTotalRecords,
    setLostItemTotalRecords,
    // Claims
    claims,
    setClaims,
    filteredClaims,
    setFilteredClaims,
    claimStatusFilter,
    setClaimStatusFilter,
    claimSearchTerm,
    setClaimSearchTerm,
    selectedClaim,
    setSelectedClaim,
    showClaimDetailModal,
    setShowClaimDetailModal,
    showCreateClaimModal,
    setShowCreateClaimModal,
    showEditClaimModal,
    setShowEditClaimModal,
    selectedClaimToEdit,
    setSelectedClaimToEdit,
    // Archives
    archives,
    setArchives,
    filteredArchives,
    setFilteredArchives,
    archiveSearchTerm,
    setArchiveSearchTerm,
    archiveCategoryFilter,
    setArchiveCategoryFilter,
    selectedArchive,
    setSelectedArchive,
    showArchiveDetailModal,
    setShowArchiveDetailModal,
    // Audit Logs
    auditLogs,
    setAuditLogs,
    filteredAuditLogs,
    setFilteredAuditLogs,
    auditLogSearchTerm,
    setAuditLogSearchTerm,
    auditLogActionFilter,
    setAuditLogActionFilter,
    auditLogUserFilter,
    setAuditLogUserFilter,
    auditLogDateFilter,
    setAuditLogDateFilter,
    // Categories
    categories,
    setCategories,
    showCreateCategoryModal,
    setShowCreateCategoryModal,
    showEditCategoryModal,
    setShowEditCategoryModal,
    selectedCategory,
    setSelectedCategory,
    // Roles
    roles,
    setRoles,
    permissions,
    setPermissions,
    showCreateRoleModal,
    setShowCreateRoleModal,
    showEditRoleModal,
    setShowEditRoleModal,
    selectedRole,
    setSelectedRole,
  };
};
