"use client";

import React from "react";
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
import useStatusesList from "@/hooks/lists/useStatusesList";
import { BoardSchema, StatusSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

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

type Props = {
    board: BoardSchema;
};

const useBoardStatusesContextState = ({
    board,
}: Props): BoardStatusesContextState => {
    const {
        statuses,
        setStatuses,
        refetch: refetchStatuses,
    } = useStatusesList({
        boardId: board.id,
    });

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

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

export const [BoardStatusesContextProvider, useBoardStatusesContext] =
    makeContext<BoardStatusesContextState, Props>(
        useBoardStatusesContextState,
        "BoardStatuses"
    );
