"use client";

import React from "react";
import { useBoardsContext } from "@/app/deals/contexts/BoardsContext";
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
import useStatusesList from "@/hooks/lists/useStatusesList";
import { StatusSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

type StatusesContextState = {
    statuses: StatusSchema[];
    setStatuses: React.Dispatch<React.SetStateAction<StatusSchema[]>>;
    refetchStatuses: () => void;
    statusesCrud: StatusesCrud;
};

const useStatusesContextState = (): StatusesContextState => {
    const { selectedBoard } = useBoardsContext();
    const {
        statuses,
        setStatuses,
        refetch: refetchStatuses,
    } = useStatusesList({
        boardId: selectedBoard?.id,
    });

    const statusesCrud = useStatusesCrud({
        statuses,
        setStatuses,
        refetchStatuses,
        boardId: selectedBoard?.id,
    });

    return {
        statuses,
        setStatuses,
        refetchStatuses,
        statusesCrud,
    };
};

export const [StatusesContextProvider, useStatusesContext] =
    makeContext<StatusesContextState>(useStatusesContextState, "Statuses");
