import { createAccessControl } from "better-auth/plugins/access";
import { adminAc, defaultStatements } from "better-auth/plugins/admin/access";

/*
When new perms are added based on there criteria make sure they are added here as well
*/

type SelectableRole = {
	label: string;
	value: string;
};

export const selectableRoles: SelectableRole[] = [
	{ label: "User", value: "user" },
	{ label: "Manager", value: "manager" },
	{ label: "Transport", value: "transport" },
	{ label: "Warehouse", value: "warehouse" },
	{ label: "Admin", value: "admin" },
	{ label: "System Admin", value: "systemAdmin" },
];

export const statement = {
	...defaultStatements,
	app: ["read", "create", "update", "delete", "readAll"],
	quality: ["read", "create", "update", "delete", "readAll"],
	logistics: ["read", "create", "update", "delete", "readAll"],
	mobile: ["read", "create", "update", "delete", "readAll"],
	openDock: ["read", "create", "update", "delete"],
	warehouse: ["read", "create", "update", "delete"],
	notifications: ["read", "create", "update", "delete", "readAll"],
} as const;

export const ac = createAccessControl(statement);

export const user = ac.newRole({
	app: ["read", "create"],
	notifications: ["read", "create"],
	openDock: ["read"],
	warehouse: ["read"],
});

export const manager = ac.newRole({
	app: ["read", "create", "update"],
	mobile: ["read", "create", "update"],
	openDock: ["read", "create", "update"],
	warehouse: ["read", "create"],
});

export const transport = ac.newRole({
	app: ["read", "create", "update"],
	openDock: ["read", "create", "update"],
	warehouse: ["read", "create"],
});

export const admin = ac.newRole({
	app: ["read", "create", "update"],
	mobile: ["read", "create", "update"],
	user: ["create", "update", "ban"],
	openDock: ["read", "create", "update"],
	warehouse: ["read", "create", "update"],
});

export const systemAdmin = ac.newRole({
	...adminAc.statements,
	app: ["read", "create", "update", "delete", "readAll"],
	quality: ["read", "create", "update", "delete", "readAll"],
	mobile: ["read", "create", "update", "delete", "readAll"],
	logistics: ["read", "create", "update", "delete", "readAll"],
	notifications: ["read", "create", "update", "delete", "readAll"],
	warehouse: ["read", "create", "update", "delete"],
	openDock: ["read", "create", "update", "delete"],
});

/*

inside a component

const { data: canImpersonate = false } = useQuery(
		permissionQuery({
			user: ["impersonate"],
			logistics: ["delete"]
		}),
	);

on the before load use this example

beforeLoad: async ({ location }) => {
		const { data: session } = await authClient.getSession();
		//const allowedRole = ["systemAdmin", "admin", "manager"];

		const canAccess = await authClient.admin.hasPermission({
			permissions: {
				opendock: ["create"],
				logistics: ["delete"]
			},
		});

		if (!session?.user) {
			throw redirect({
				to: "/",
				search: {
					redirect: location.href,
				},
			});
		}

		//if (!allowedRole.includes(session.user.role as string)) {

		if (!canAccess) {
			throw redirect({
				to: "/",
			});
		}

		return { user: session.user };
	},

	*/
