import {
  hotelTarfiDetail,
  hotelTarifCreate,
  hotelTarifDelete,
  hoteltarifUpdate,
} from "@/pages/tours/lib/api";
import { TarifColumns } from "@/pages/tours/lib/column";
import type { Tarif } from "@/pages/tours/lib/type";
import { Button } from "@/shared/ui/button";
import { Dialog, DialogContent } from "@/shared/ui/dialog";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/shared/ui/form";
import { Input } from "@/shared/ui/input";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/shared/ui/table";
import RealPagination from "@/widgets/real-pagination/ui/RealPagination";
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  flexRender,
  getCoreRowModel,
  useReactTable,
} from "@tanstack/react-table";
import { Loader, PlusIcon } from "lucide-react";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import z from "zod";

const formSchema = z.object({
  name: z.string().min(1, { message: "Majburiy maydon" }),
  name_ru: z.string().min(1, { message: "Majburiy maydon" }),
});

const TarifTable = ({
  data,
  page,
  pageSize,
}: {
  page: number;
  pageSize: number;
  data:
    | {
        links: {
          previous: string;
          next: string;
        };
        total_items: number;
        total_pages: number;
        page_size: number;
        current_page: number;
        results: Tarif[];
      }
    | undefined;
}) => {
  const { t } = useTranslation();
  const [open, setOpen] = useState<boolean>(false);
  const [editId, setEditId] = useState<number | null>(null);
  const queryClient = useQueryClient();
  const [types, setTypes] = useState<"edit" | "create">("create");

  const handleEdit = (id: number) => {
    setTypes("edit");
    setOpen(true);
    setEditId(id);
  };

  const { data: tarifDetail } = useQuery({
    queryKey: ["tarif_badge", editId],
    queryFn: () => hotelTarfiDetail({ id: editId! }),
    enabled: !!editId,
  });

  const { mutate: deleteMutate } = useMutation({
    mutationFn: ({ id }: { id: number }) => hotelTarifDelete({ id }),
    onSuccess: () => {
      queryClient.refetchQueries({ queryKey: ["tarif_badge"] });
      queryClient.refetchQueries({ queryKey: ["all_tarif"] });
      setOpen(false);
      form.reset();
    },
    onError: () => {
      toast.error(t("Xatolik yuz berdi"), {
        position: "top-center",
        richColors: true,
      });
    },
  });

  const handleDelete = (id: number) => {
    deleteMutate({ id });
  };

  const columns = TarifColumns(handleEdit, handleDelete, t);

  const { mutate: create, isPending } = useMutation({
    mutationFn: ({ body }: { body: { name: string; name_ru: string } }) =>
      hotelTarifCreate({ body }),
    onSuccess: () => {
      queryClient.refetchQueries({ queryKey: ["tarif_badge"] });
      queryClient.refetchQueries({ queryKey: ["all_tarif"] });
      setOpen(false);
      form.reset();
    },
    onError: () => {
      toast.error(t("Xatolik yuz berdi"), {
        position: "top-center",
        richColors: true,
      });
    },
  });

  const { mutate: update, isPending: updatePending } = useMutation({
    mutationFn: ({
      body,
      id,
    }: {
      id: number;
      body: { name: string; name_ru: string };
    }) => hoteltarifUpdate({ body, id }),
    onSuccess: () => {
      queryClient.refetchQueries({ queryKey: ["tarif_badge"] });
      queryClient.refetchQueries({ queryKey: ["all_tarif"] });
      setOpen(false);
      form.reset();
    },
    onError: () => {
      toast.error(t("Xatolik yuz berdi"), {
        position: "top-center",
        richColors: true,
      });
    },
  });

  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      name: "",
      name_ru: "",
    },
  });

  useEffect(() => {
    if (tarifDetail) {
      form.setValue("name", tarifDetail.data.data.name);
    }
  }, [editId, tarifDetail]);

  function onSubmit(values: z.infer<typeof formSchema>) {
    if (types === "create") {
      create({
        body: {
          name: values.name,
          name_ru: values.name_ru,
        },
      });
    } else if (types === "edit" && editId) {
      update({
        id: editId,
        body: {
          name: values.name,
          name_ru: values.name_ru,
        },
      });
    }
  }

  const table = useReactTable({
    data: data?.results ?? [],
    columns,
    getCoreRowModel: getCoreRowModel(),
    manualPagination: true,
    pageCount: data?.total_pages ?? 0,
    state: {
      pagination: {
        pageIndex: page - 1,
        pageSize: pageSize,
      },
    },
  });
  return (
    <>
      <div className="flex justify-end">
        <Button variant="default" disabled>
          <PlusIcon className="mr-2" />
          {t("Qo‘shish")}
        </Button>
      </div>

      <div className="border border-gray-700 rounded-md overflow-hidden mt-4">
        <Table key={data?.current_page}>
          <TableHeader>
            {table.getHeaderGroups().map((headerGroup) => (
              <TableRow key={headerGroup.id}>
                {headerGroup.headers.map((header) => (
                  <TableHead key={header.id}>
                    {header.isPlaceholder
                      ? null
                      : flexRender(
                          header.column.columnDef.header,
                          header.getContext(),
                        )}
                  </TableHead>
                ))}
              </TableRow>
            ))}
          </TableHeader>

          <TableBody>
            {table.getRowModel().rows?.length ? (
              table.getRowModel().rows.map((row) => (
                <TableRow key={row.id}>
                  {row.getVisibleCells().map((cell) => (
                    <TableCell key={cell.id}>
                      {flexRender(
                        cell.column.columnDef.cell,
                        cell.getContext(),
                      )}
                    </TableCell>
                  ))}
                </TableRow>
              ))
            ) : (
              <TableRow>
                <TableCell
                  colSpan={columns.length}
                  className="h-24 text-center text-gray-400"
                >
                  {t("Ma'lumot topilmadi")}
                </TableCell>
              </TableRow>
            )}
          </TableBody>
        </Table>
      </div>

      <RealPagination
        table={table}
        totalPages={data?.total_pages}
        namePage="pageTarif"
        namePageSize="pageTarifSize"
      />

      <Dialog open={open} onOpenChange={setOpen}>
        <DialogContent>
          <p className="text-xl">
            {types === "create" ? t("Saqlash") : t("Tahrirlash")}
          </p>
          <Form {...form}>
            <form
              onSubmit={form.handleSubmit(onSubmit)}
              className="space-y-6 p-2"
            >
              <FormField
                control={form.control}
                name="name"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>{t("Nomi")}</FormLabel>
                    <FormControl>
                      <Input placeholder={t("Nomi")} {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />
              <FormField
                control={form.control}
                name="name_ru"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>{t("Nomi (ru)")}</FormLabel>
                    <FormControl>
                      <Input placeholder={t("Nomi (ru)")} {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <div className="flex justify-end gap-4">
                <Button
                  type="button"
                  onClick={() => setOpen(false)}
                  className="border-slate-600 text-white hover:bg-gray-600 bg-gray-600"
                >
                  {t("Bekor qilish")}
                </Button>
                <Button type="submit">
                  {isPending || updatePending ? (
                    <Loader className="animate-spin" />
                  ) : types === "create" ? (
                    t("Saqlash")
                  ) : (
                    t("Tahrirlash")
                  )}
                </Button>
              </div>
            </form>
          </Form>
        </DialogContent>
      </Dialog>
    </>
  );
};

export default TarifTable;
