import { useUserStore } from "@/stores/user"
import { computed } from "vue";
import { Role } from "@shared/types/roles"

export function useAuth() {
    const userStore = useUserStore();

    // Account status control
    const accountStatus = computed(() => userStore.state);

    // RBAC
    const roles = computed<string[]>(() => {
        return userStore.user?.roleData?.map((r: Role) => r.name) ?? [];
    });

    function isDev() {
        return roles.value.includes('Dev');
    }

    function hasRole(roleName: string): boolean {
        if (isDev()) return true;
        return roles.value.includes(roleName);
    }

    function hasAnyRole(roleNames: string[]): boolean {
        if (isDev()) return true;
        return roles.value.some(name => roleNames.includes(name))
    }

    function hasAllRoles(roleNames: string[]): boolean {
        if (isDev()) return true;
        return roles.value.every(name => roleNames.includes(name))
    }

    return { hasRole, hasAnyRole, hasAllRoles, accountStatus }
}