import { computed } from 'vue';
import { useStore } from 'vuex';

/**
 * Composable for permission checks in Vue components
 */
export function usePermissions() {
  const store = useStore();

  const permissions = computed(() => store.getters.currentPermissions);
  const isOwner = computed(() => store.getters.isClubOwner);
  const userRole = computed(() => store.getters.userRole);

  /**
   * Check if user has specific permission
   * @param {string} resource - Resource name (diary, members, teams, etc.)
   * @param {string} action - Action type (read, write, delete)
   * @returns {boolean}
   */
  const can = (resource, action = 'read') => {
    return store.getters.hasPermission(resource, action);
  };

  /**
   * Check if user can read
   */
  const canRead = (resource) => can(resource, 'read');

  /**
   * Check if user can write
   */
  const canWrite = (resource) => can(resource, 'write');

  /**
   * Check if user can delete
   */
  const canDelete = (resource) => can(resource, 'delete');

  /**
   * Check if user is admin (owner or admin role)
   */
  const isAdmin = computed(() => {
    return isOwner.value || userRole.value === 'admin';
  });

  /**
   * Check if user has specific role
   */
  const hasRole = (role) => {
    return userRole.value === role;
  };

  return {
    permissions,
    isOwner,
    isAdmin,
    userRole,
    can,
    canRead,
    canWrite,
    canDelete,
    hasRole
  };
}


