import { createContext, useContext } from "react";
import type { RinnoviId } from "~/schemas/public/Rinnovi";
import type { UsersId } from "~/schemas/public/Users";
import type { RinnovoData } from "~/server/services/rinnovi.service";

type RinnovoProviderProps = {
	isAdmin: boolean;
	userId: UsersId;
	rinnovoId: RinnoviId;
	rinnovo: RinnovoData;
	children: React.ReactNode;
};
type RinnovoContextProps = {
	isAdmin: boolean;
	userId: UsersId;
	rinnovoId: RinnoviId;
	rinnovo: RinnovoData;
};

const RinnovoContext = createContext<RinnovoContextProps | null>(null);

export const RinnovoProvider = ({
	children,
	...props
}: RinnovoProviderProps) => {
	return (
		<RinnovoContext.Provider value={{ ...props }}>
			{children}
		</RinnovoContext.Provider>
	);
};

export const useRinnovo = () => {
	const rinnovoContext = useContext(RinnovoContext);
	if (!rinnovoContext) {
		throw new Error("useRinnovo must be used within a RinnovoProvider");
	}
	return rinnovoContext;
};
