import type { IuserInformations } from "@/utils/requests/types";
import { createContext, useContext, useState } from "react";
import type { IauthenticationData } from "./Types";
export interface UserAuthenticationContextProps {
	isAuthenticated: boolean;
	setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
	setAuthenticationData: React.Dispatch<React.SetStateAction<IauthenticationData>>;
	userInformations: IuserInformations;
	setUserInformations: React.Dispatch<React.SetStateAction<IuserInformations>>;
}

export const UserAuthenticationContext = createContext<UserAuthenticationContextProps>({
	isAuthenticated: false,
	setIsAuthenticated: () => {},
	setAuthenticationData: () => {},
	userInformations: {
		username: "",
		email: "",
		firstName: "",
		lastName: "",
		marchand: {
			// biome-ignore lint/style/useNamingConvention: <Api reponse>
			marchand_id: "",
			nom: "",
			code: "",
			adresse: "",
			urlSuccess: "",
			urlEchec: "",
			entreprise: 0,
			user: 0,
		},
	},
	setUserInformations: () => {},
});

export const UserAuthenticationContextProvider = ({ children }: { children: React.ReactNode }) => {
	const [isAuthenticated, setIsAuthenticated] = useState(false);
	const [authenticationData, setAuthenticationData] = useState<IauthenticationData>({
		access: "",
		refresh: "",
	});
	const [userInformations, setUserInformations] = useState<IuserInformations>({
		username: "JohnDoe",
		email: "JohnDoe@example.com",
		firstName: "John",
		lastName: "Doe",
		marchand: {
			// biome-ignore lint/style/useNamingConvention: <Api response>
			marchand_id: "id123",
			nom: "Beasy",
			code: "BEASY-EXAMPLE-1",
			adresse: "Plateau 2, 1023, Immeuble Chardy",
			urlSuccess: "https://example.com/success",
			urlEchec: "https://example.com/echec",
			entreprise: 0,
			user: 0,
		},
	});

	return (
		<UserAuthenticationContext.Provider
			value={{
				isAuthenticated: isAuthenticated,
				setIsAuthenticated,
				setAuthenticationData,
				userInformations,
				setUserInformations,
			}}
		>
			{children}
		</UserAuthenticationContext.Provider>
	);
};

export const useUserAuthenticationContext = () => {
	return useContext(UserAuthenticationContext);
};
