import faker from 'faker';
import tjanster from './tjanster.js';
import utforandeVerksamheter from './utforande-verksamheter.js';
import chooseRandom from './utils/choose-random.util.js';

faker.locale = 'sv';

const TJANSTER = tjanster.generate();
const UTFORANDEVERKSAMHETER = utforandeVerksamheter.generate();
const ROLES = ['MSFA_AuthAdmin', 'MSFA_ReceiveDeltagare', 'MSFA_ReportAndPlanning'];

function generateEmployees(amount = 10) {
  const employees = [];

  for (let i = 1; i <= amount; ++i) {
    const firstName = faker.name.firstName();
    const lastName = faker.name.lastName();
    const currentTjanster = chooseRandom(TJANSTER, faker.datatype.number({ min: 1, max: TJANSTER.length }));
    const currentUtforandeVerksamheter = chooseRandom(UTFORANDEVERKSAMHETER, faker.datatype.number({ min: 1, max: 5 }));
    const hasBehorigheter = Math.random() > 0.1;
    const id = faker.datatype.uuid();

    const employee = {
      id,
      ciamUserId: id,
      firstName,
      lastName,
      name: `${firstName} ${lastName}`,
      personnummer: `${faker.date
        .between('1950', '2000')
        .toISOString()
        .split('T')[0]
        .replace(/-/g, '')}-${faker.datatype.number({
        min: 1000,
        max: 9999,
      })}`,
      email: faker.internet.email(firstName.toLowerCase(), lastName.toLowerCase()),
      roles: hasBehorigheter
        ? ['MSFA_Standard', ...chooseRandom(ROLES, faker.datatype.number({ min: 1, max: ROLES.length }))]
        : ['MSFA_Standard'],
      tjanst: hasBehorigheter ? currentTjanster.map(tjanst => tjanst.label) : [],
      tjansteKoder: hasBehorigheter ? currentTjanster.map(tjanst => tjanst.id) : [],
      allaUtforandeVerksamheter: faker.datatype.boolean(),
      utforandeVerksamhet: hasBehorigheter
        ? currentUtforandeVerksamheter.map(utforandeVerksamhet => utforandeVerksamhet)
        : [],
      utforandeVerksamhetIds: hasBehorigheter
        ? currentUtforandeVerksamheter.map(utforandeVerksamhet => utforandeVerksamhet.utforandeVerksamhetId)
        : [],
    };

    employees.push(employee);
  }

  console.info('Employees generated...');
  return employees;
}

export default {
  generate: generateEmployees,
};
