import { describe, expect, it } from 'vitest';
import {
  getUserDisplayName,
  toStaffProfile,
} 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',
    staffProfile: {
      id: 'staff-1',
      employee_number: 'E-1',
      job_title: 'Teacher',
      staff_type: 'instructional',
      status: 'active',
      organizationId: 'org-1',
      campusId: 'campus-1',
      userId: 'user-1',
    },
    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 staff profile with backend staff id and campus name', () => {
    expect(toStaffProfile(createUser())).toEqual({
      id: 'staff-1',
      full_name: 'Ava Lee',
      role: 'teacher',
      campus: 'North Campus',
      avatar_url: null,
    });
  });

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