import { DEFAULT_PRODUCT_ROLE, isUserRole } from '@/shared/constants/roles';
import { DEFAULT_CAMPUS_LABEL } from '@/shared/constants/campusDisplay';
import { CurrentUser } from '@/shared/types/auth';
import { StaffProfile, UserRole } from '@/shared/types/app';

function getProductRole(user: CurrentUser): UserRole {
  const name = user.app_role?.name;
  return isUserRole(name) ? name : DEFAULT_PRODUCT_ROLE;
}

export function getUserDisplayName(user: CurrentUser): string {
  const fullName = [user.firstName, user.lastName].filter(Boolean).join(' ').trim();

  return fullName || user.email;
}

export function toStaffProfile(user: CurrentUser): StaffProfile {
  return {
    id: user.staffProfile?.id || user.id,
    full_name: getUserDisplayName(user),
    role: getProductRole(user),
    campus: user.campus?.name || user.campus?.code || DEFAULT_CAMPUS_LABEL,
    avatar_url: null,
  };
}
