import { useState } from "react";

interface Entity {
  id: string;
  name: string;
}

interface Employee {
  id: string;
  name: string;
  email?: string;
  role?: string;
  title?: string;
  phone?: string;
  inviteStatus?: "Pending" | "Accepted" | "Not Invited";
}

interface UseActionHandlersProps {
  selectedOrgId: string;
  selectedUnitId: string;
  toast: (data: { title: string; description?: string }) => void;
}

export const useActionHandlers = ({
  selectedOrgId,
  selectedUnitId,
  toast: externalToast,
}: UseActionHandlersProps) => {
  const [editUnitDialogOpen, setEditUnitDialogOpen] = useState(false);
  const [manageUnitAdminDialogOpen, setManageUnitAdminDialogOpen] =
    useState(false);
  const [deleteUnitDialogOpen, setDeleteUnitDialogOpen] = useState(false);
  const [permanentDeleteUnitDialogOpen, setPermanentDeleteUnitDialogOpen] = useState(false);
  const [addFromSubCityDialogOpen, setAddFromSubCityDialogOpen] = useState(false);
  const [addDepartmentDialogOpen, setAddDepartmentDialogOpen] = useState(false);
  const [editDepartmentDialogOpen, setEditDepartmentDialogOpen] =
    useState(false);
  const [deleteDepartmentDialogOpen, setDeleteDepartmentDialogOpen] =
    useState(false);
    const [moveDepartmentDialogOpen, setMoveDepartmentDialogOpen] =
      useState(false);
  const [addSubDepartmentDialogOpen, setAddSubDepartmentDialogOpen] =
    useState(false);
  const [addUserUnderDepartmentOpen, setAddUserUnderDepartmentOpen] =
    useState(false);
  const [addUserDialogOpen, setAddUserDialogOpen] = useState(false);
  const [viewUsersDialogOpen, setViewUsersDialogOpen] = useState(false);
  const [assignUserDialogOpen, setAssignUserDialogOpen] = useState(false);
  const [deleteTeamMemberDialog, setDeleteTeamMemberDialog] = useState(false);
  const [currentDepartmentId, setCurrentDepartmentId] = useState("");
  const [currentDepartmentName, setCurrentDepartmentName] = useState("");
  const [currentTeamMemberId, setCurrentTeamMemberId] = useState("");
  const [currentTeamMemberName, setCurrentTeamMemberName] = useState("");
  const[deactivateTeamMemberDialog,setDeactivateTeamMemberDialog] = useState(false)
  const [currentOrganizationId, setCurrentOrganizationId] = useState("");
  const [currentUnitId, setCurrentUnitId] = useState("");
  const [currentUnitName, setCurrentUnitName] = useState("");
  const[currentUserId, setCurrentUserId] = useState("")

  const toast = (data: { title: string; description?: string }) => {
    externalToast(data);
  };
  return {
    handleSelectOrganization: (id: string) => {
      setCurrentOrganizationId(id);
    },
    unit: {
      onAddDepartment: () => {
        setCurrentOrganizationId(currentOrganizationId);
        setCurrentUnitId(selectedUnitId);
        setAddDepartmentDialogOpen(true);
      },
      onManageUnitAdmin: (unitId: string) => {
        setCurrentOrganizationId(selectedOrgId);
        setCurrentUnitId(unitId || selectedUnitId);
        setManageUnitAdminDialogOpen(true);
      },
      onEditUnit: (unitId: string) => {
        setCurrentUnitId(unitId || selectedUnitId);
        setEditUnitDialogOpen(true);
      },
      onArchiveUnit: (departmentId: string, departmentName: string) => {
        setCurrentUnitId(departmentId);
        setCurrentUnitName(departmentName);
        setDeleteUnitDialogOpen(true);
      },
      onDeleteUnit: (departmentId: string, departmentName: string) => {
        setCurrentUnitId(departmentId);
        setCurrentUnitName(departmentName);
        setPermanentDeleteUnitDialogOpen(true);
      },
      onAddFromSubCity: (unitId: string) => {
        setCurrentUnitId(unitId);
        setAddFromSubCityDialogOpen(true);
      },
    },
    department: {
      onAddUserUnderDepartment: (
        departmentId: string,
        departmentName: string
      ) => {
        setCurrentDepartmentId(departmentId);
        setCurrentDepartmentName(departmentName);
        setAddUserUnderDepartmentOpen(true);
      },
      onEditDepartment: (departmentId: string, departmentName: string) => {
        setCurrentDepartmentId(departmentId);
        setCurrentDepartmentName(departmentName);
        setEditDepartmentDialogOpen(true);
      },
      onDeleteDepartment: (departmentId: string, departmentName: string) => {
        setCurrentDepartmentId(departmentId);
        setCurrentDepartmentName(departmentName);
        setDeleteDepartmentDialogOpen(true);
      },
      onMoveDepartment: (departmentId: string, departmentName: string) => {
     
        setCurrentDepartmentId(departmentId);
        setCurrentDepartmentName(departmentName);
        setMoveDepartmentDialogOpen(true);

      },
      onViewUsers: () => toast({ title: "View Users" }),
      onAddSubDepartment: (departmentId: string, departmentName: string) => {
        setCurrentDepartmentId(departmentId);
        setCurrentOrganizationId(selectedOrgId);
        setCurrentUnitId(selectedUnitId);
        setCurrentDepartmentName(departmentName);
        setAddSubDepartmentDialogOpen(true);
      },
      onAssignUser: (departmentId: string, departmentName: string) => {
        setCurrentDepartmentId(departmentId);
        setCurrentDepartmentName(departmentName);
        setCurrentOrganizationId(selectedOrgId);
        setCurrentUnitId(selectedUnitId);
        setAssignUserDialogOpen(true);
      },
      onDelegate: () => toast({ title: "Delegate" }),
    },
    teamMember: {
      onDeleteTeamMember: (
        departmentId: string,
        departmentName: string,
        teamMemberId: string,
        teamMemberName: string
      ) => {
        setCurrentDepartmentId(departmentId);
        setCurrentDepartmentName(departmentName);
        setDeleteTeamMemberDialog(true);
        setCurrentTeamMemberId(teamMemberId);
        setCurrentTeamMemberName(teamMemberName);
      },
      
        onDeactivateTeamMember:(
          userId:string,
          teamMemberName: string
        )=>{
          setCurrentUserId(userId);
          setCurrentTeamMemberName(teamMemberName)
          setDeactivateTeamMemberDialog(true)
        }
    },
    dialogs: {
      manageUnitAdmin: {
        isOpen: manageUnitAdminDialogOpen,
        onClose: () => setManageUnitAdminDialogOpen(false),
        organizationId: currentOrganizationId,
        unitId: currentUnitId,
      },
      editUnit: {
        isOpen: editUnitDialogOpen,
        onClose: () => setEditUnitDialogOpen(false),
        unitId: currentUnitId,
      },
      deleteUnit: {
        isOpen: deleteUnitDialogOpen,
        onClose: () => setDeleteUnitDialogOpen(false),
        unitId: currentUnitId,
        unitName: currentUnitName,
      },
      permanentDeleteUnit: {
        isOpen: permanentDeleteUnitDialogOpen,
        onClose: () => setPermanentDeleteUnitDialogOpen(false),
        unitId: currentUnitId,
        unitName: currentUnitName,
      },
      addFromSubCity: {
        isOpen: addFromSubCityDialogOpen,
        onClose: () => setAddFromSubCityDialogOpen(false),
        parentUnitId: currentUnitId,
      },
      department: {
        isOpen: addDepartmentDialogOpen,
        onClose: () => setAddDepartmentDialogOpen(false),
        organizationId: currentOrganizationId,
        unitId: currentUnitId,
      },
      editDepartment: {
        isOpen: editDepartmentDialogOpen,
        onClose: () => setEditDepartmentDialogOpen(false),
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
      },
      deleteDepartment: {
        isOpen: deleteDepartmentDialogOpen,
        onClose: () => setDeleteDepartmentDialogOpen(false),
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
      },
      moveDepartment: {
        isOpen: moveDepartmentDialogOpen,
        onClose: () => setMoveDepartmentDialogOpen(false),
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
      },
      userUnderDepartment: {
        isOpen: addUserUnderDepartmentOpen,
        onClose: () => setAddUserUnderDepartmentOpen(false),
        unitId: selectedUnitId,
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
      },
      subDepartment: {
        isOpen: addSubDepartmentDialogOpen,
        onClose: () => setAddSubDepartmentDialogOpen(false),
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
        organizationId: currentOrganizationId,
        unitId: currentUnitId,
      },
      user: {
        unitId: currentUnitId,
        isOpen: addUserDialogOpen,
        onClose: () => setAddUserDialogOpen(false),
      },
      viewUsers: {
        unitId: currentUnitId,
        isOpen: viewUsersDialogOpen,
        onClose: () => setViewUsersDialogOpen(false),
      },
      assignUser: {
        isOpen: assignUserDialogOpen,
        onClose: () => setAssignUserDialogOpen(false),
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
        organizationId: currentOrganizationId,
        unitId: currentUnitId,
      },
      deleteTeamMember: {
        isOpen: deleteTeamMemberDialog,
        onClose: () => setDeleteTeamMemberDialog(false),
        departmentId: currentDepartmentId,
        departmentName: currentDepartmentName,
        teamMemberId: currentTeamMemberId,
        teamMemberName: currentTeamMemberName,
      },
      deactivateTeamMember:{
        isOpen: deactivateTeamMemberDialog,
        onClose: ()=> setDeactivateTeamMemberDialog(false),
        userId: currentUserId,
        teamMemberName: currentTeamMemberName
      }
    },
  };
};
