"use client";

import { apiDefaultParams } from "@/constants/Api_Params";
import { AdminStatus } from "@/constants/enums";
import {
  useChangeAdminStatus,
  useDeleteAdmin,
  useGetAdminListQuery,
} from "@/hooks/queries";
import { IUser, TChangeAdminStatusCredentials } from "@/lib/api";
import { deleteEmptyKeys } from "@/utils/shared";
import { useIsMutating } from "@tanstack/react-query";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";

export const useAdminsPresenter = () => {
  const [isChangeStatusModalOpen, setIsChangeStatusModalOpen] = useState(false);
  const [isFilterModalOpen, setIsFilterModalOpen] = useState(false);
  const [currentUser, setCurrentUser] = useState<IUser | null>(null);
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [status, setStatus] = useState<string>(
    currentUser?.status ?? AdminStatus.ACTIVE,
  );
  const isMutating = useIsMutating();
  const router = useRouter();
  const searchParams = useSearchParams();
  const {
    register: filterRegister,
    handleSubmit: handleFilterSubmit,
    reset: filterFormReset,
    watch,
    setValue: setFilterValue,
  } = useForm();

  const {
    register,
    formState: { errors },
    handleSubmit,
  } = useForm<TChangeAdminStatusCredentials>({
    mode: "onChange",
    defaultValues: {
      status: currentUser?.status,
    },
  });
  const [params, setParams] = useState<Record<string, any>>({});
  const pathName = usePathname();

  useEffect(() => {
    const newParams: Record<string, any> = {
      ...apiDefaultParams,
      offset: 0,
      limit: 10,
    };
    for (const [key, value] of searchParams.entries()) {
      newParams[key] = value;
    }
    setParams(newParams);
    filterFormReset(newParams);
  }, [searchParams, filterFormReset]);
  const { data, isPending } = useGetAdminListQuery(params);
  const { mutate: deleteAdmin } = useDeleteAdmin(() => {
    setIsModalOpen(false);
  });

  const onConfirmDelete = () => {
    if (currentUser) {
      deleteAdmin(currentUser.id);
    }
  };
  const { mutate: changeStatus } = useChangeAdminStatus({
    id: currentUser?.id ?? "",
    successCallback: () => {
      setIsChangeStatusModalOpen(false);
    },
  });

  const search = (data: any) => {
    const newParams = { ...params, ...data, offset: 0 };
    const cleanedParams = deleteEmptyKeys(newParams);
    const queryString = new URLSearchParams(cleanedParams).toString();
    router.push(`${pathName}?${queryString}`);
    setIsFilterModalOpen(false);
  };
  const allUsers = data?.data?.users ?? [];
  const metadata = data?.meta;
  const columns = [
    "row",
    "full name",
    "email",

    "username",
    "status",
    "actions",
  ];
  return {
    allUsers,
    setFilterValue,
    metadata,
    columns,
    isPending,
    onConfirmDelete,
    isModalOpen,
    setIsModalOpen,
    isMutating,
    router,
    setCurrentUser,
    pathName,
    status,
    setStatus,
    changeStatus,
    isChangeStatusModalOpen,
    currentUser,
    setIsChangeStatusModalOpen,
    register,
    errors,
    handleSubmit,
    isFilterModalOpen,
    setIsFilterModalOpen,
    setParams,
    filterRegister,
    handleFilterSubmit,
    search,
    watch,
  };
};
