import { describe, expect, it } from 'vitest';
import {
  getUserDisplayName,
  toUserProfile,
} from '@/business/auth/mappers';
import { DEFAULT_CAMPUS_LABEL } from '@/shared/constants/campusDisplay';
import type { CurrentUser } from '@/shared/types/auth';

function createUser(overrides: Partial<CurrentUser> = {}): CurrentUser {
  return {
    id: 'user-1',
    email: 'teacher@example.com',
    firstName: 'Ava',
    lastName: 'Lee',
    app_role: { name: 'teacher' },
    organizations: null,
    organizationId: 'org-1',
    campus: { id: 'campus-1', name: 'North Campus', code: 'north' },
    campusId: 'campus-1',
    avatar: '/uploads/avatar.png',
    permissions: [],
    ...overrides,
  };
}

describe('auth mappers', () => {
  it('formats display names and falls back to email', () => {
    expect(getUserDisplayName(createUser())).toBe('Ava Lee');
    expect(getUserDisplayName(createUser({ firstName: null, lastName: null }))).toBe('teacher@example.com');
  });

  it('maps current user to UI profile with user id and campus name', () => {
    expect(toUserProfile(createUser())).toEqual({
      id: 'user-1',
      full_name: 'Ava Lee',
      role: 'teacher',
      campus: 'North Campus',
      avatar_url: '/uploads/avatar.png',
    });
  });

  it('uses user id and default campus label when optional backend profile data is absent', () => {
    expect(toUserProfile(createUser({
      campus: null,
      campusId: null,
      app_role: { name: 'support_staff' },
      avatar: null,
    }))).toEqual({
      id: 'user-1',
      full_name: 'Ava Lee',
      role: 'support_staff',
      campus: DEFAULT_CAMPUS_LABEL,
      avatar_url: null,
    });
  });
});
