import { describe, expect, it } from 'vitest';
import {
  getSubmitReadinessIssues,
  normalizeEmployeeIdInput,
  validateEmployeeIdFormat,
} from './staffProfileValidation';
import type { StaffProfileDto } from './types';

const baseStaff = (): StaffProfileDto => ({
  employeeId: 'EMP-001',
  academicTitleCode: 'master',
  academicTitleOther: null,
  unitId: '00000000-0000-4000-8000-000000000099',
  unitNameFreetext: null,
  jobTitle: 'Giảng viên',
  profileVerificationStatus: 'draft',
  verificationSubmittedAt: null,
  verifiedAt: null,
  verifiedByUserId: null,
  rejectionReason: null,
  version: 1,
});

describe('normalizeEmployeeIdInput', () => {
  it('uppercases and trims', () => {
    expect(normalizeEmployeeIdInput('  cb-1  ')).toBe('CB-1');
  });
  it('returns null for empty', () => {
    expect(normalizeEmployeeIdInput('')).toBeNull();
    expect(normalizeEmployeeIdInput(null)).toBeNull();
  });
});

describe('validateEmployeeIdFormat', () => {
  it('accepts valid pattern', () => {
    expect(validateEmployeeIdFormat('ABC-123')).toBeNull();
  });
  it('rejects too short', () => {
    expect(validateEmployeeIdFormat('AB')).not.toBeNull();
  });
});

describe('getSubmitReadinessIssues', () => {
  it('returns empty when complete', () => {
    expect(getSubmitReadinessIssues(baseStaff())).toEqual([]);
  });
  it('flags missing employee id', () => {
    const s = baseStaff();
    s.employeeId = null;
    expect(getSubmitReadinessIssues(s).some((x) => x.includes('nhân sự'))).toBe(true);
  });
  it('flags other without text', () => {
    const s = baseStaff();
    s.academicTitleCode = 'other';
    s.academicTitleOther = '';
    expect(getSubmitReadinessIssues(s).some((x) => x.includes('Khác'))).toBe(true);
  });
});
