"use client";

import React from "react";
import { useStatusesContext } from "@/app/deals/contexts/StatusesContext";
import { DealsCrud, useDealsCrud } from "@/hooks/cruds/useDealsCrud";
import useDealsList from "@/hooks/lists/useDealsList";
import { DealSchema, PaginationInfoSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

type DealsContextState = {
    deals: DealSchema[];
    setDeals: React.Dispatch<React.SetStateAction<DealSchema[]>>;
    refetchDeals: () => void;
    dealsCrud: DealsCrud;
    paginationInfo: PaginationInfoSchema;
    page: number;
    setPage: React.Dispatch<React.SetStateAction<number>>;
};

type Props = {
    withPagination?: boolean;
    boardId?: number;
    projectId?: number;
};

const useDealsContextState = ({
    withPagination = false,
    boardId,
    projectId,
}: Props): DealsContextState => {
    const { statuses } = useStatusesContext();
    const dealsListObjects = useDealsList({
        boardId,
        projectId,
        withPagination,
    });

    const dealsCrud = useDealsCrud({
        ...dealsListObjects,
        boardId,
        statuses,
    });

    return {
        ...dealsListObjects,
        dealsCrud,
    };
};

export const [DealsContextProvider, useDealsContext] = makeContext<
    DealsContextState,
    Props
>(useDealsContextState, "Deals");
