import { redirect } from "@tanstack/react-router";
import {
	adminClient,
	genericOAuthClient,
	usernameClient,
} from "better-auth/client/plugins";

import { createAuthClient } from "better-auth/react";
import {
	ac,
	admin,
	manager,
	systemAdmin,
	transport,
	user,
} from "./auth-permissions";

export const authClient = createAuthClient({
	baseURL: `${window.location.origin}/lst/api/auth`,
	plugins: [
		adminClient({
			ac,
			roles: {
				admin,
				user,
				manager,
				transport,
				systemAdmin,
			},
		}),
		genericOAuthClient(),
		usernameClient(),
	],
	fetchOptions: {
		onError() {
			redirect({
				to: "/app-down",
				replace: true,
			});
		},
	},
});

export const { useSession, signUp, signIn, signOut } = authClient;
