const { snakeCase } = require('snake-case');

const ADMIN_TEMPLATE = `
// idx start

$user = User::factory()->create([
    'name' => 'William',
    'email' => 'william_lee@school.com',
    'password' => 'william_lee',
    'role' => 'admin',
    'role_id' => ''
]);

Admin::factory()->create([
    'name' => 'William',
    'firstName' => 'William',
    'lastName' => 'Lee',
    'dateOfBirth' => '01-01-1980',
    'gender' => 'female',
    'email' => 'william_lee@school.com',
    'contact' => 'address_here',
    'skills' => 'skill_here',
    'user_id'=> $user->id,
    'avatar'=>'_avatar_'
]);
`.trim();

const STUDENT_TEMPLATE = `
// idx start

$user = User::factory()->create([
    'name' => 'William',
    'email' => 'william_lee@school.com',
    'password' => 'william_lee',
    'role' => 'student',
    'role_id' => ''
]);

Student::factory()->create([
    'name' => 'William',
    'firstName' => 'William',
    'lastName' => 'Lee',
    'dateOfBirth' => '01-01-1980',
    'gender' => 'female',
    'email' => 'william_lee@school.com',
    'address' => 'address_here',
    'user_id'=> $user->id,
    'avatar'=>'_avatar_'
]);
`.trim();

const TEACHER_TEMPLATE = `
// idx start

$user = User::factory()->create([
    'name' => 'William',
    'email' => 'william_lee@school.com',
    'password' => 'william_lee',
    'role' => 'teacher',
    'role_id' => ''
]);

Teacher::factory()->create([
    'name' => 'William',
    'firstName' => 'William',
    'lastName' => 'Lee',
    'dateOfBirth' => '01-01-1980',
    'gender' => 'female',
    'email' => 'william_lee@school.com',
    'contact' => 'address_here',
    'skills' => 'skill_here',
    'user_id'=> $user->id,
    'avatar'=>'_avatar_'
]);
`.trim();

function getRandomInt(max) {
    return Math.floor(Math.random() * max);
}

const fs = require('fs');

const ac_list = fs.readFileSync('./ac_list.txt', { encoding: 'utf-8' }).trim().split('\n');
const entries = ac_list.map(a => a.split('#'));

const names = entries.map(e => e[0]);
const passwords = entries.map(e => e[0].replace(' ', '_').toLowerCase());
const firstNames = names.map(e => e.split(' ')[0]);
const lastNames = names.map(e => e.split(' ')[1]);
const emails = entries.map(e => e[1]);
const genders = entries.map(e => e[2]);
const addresses = entries.map(e => e[3]);
const skills = entries.map(e => e[4]);

const mod_admin = entries.map((e, idx) => {
    var num_of_skills = getRandomInt(5) + 1;
    var skill_list = [];
    for (i = 0; i < num_of_skills; i++) {
        skill_list.push(skills[getRandomInt(20)]);
    }
    return ADMIN_TEMPLATE.replace(/idx/g, idx)
        .replace(/william_lee@school.com/g, emails[idx])
        .replace(/address_here/g, addresses[idx])
        .replace(/female/g, genders[idx])
        .replace(/skill_here/g, skill_list.join(', '))
        .replace(/william_lee/g, passwords[idx])
        .replace(/_avatar_/g, snakeCase(emails[idx]) + '.jpg')
        .replace(/William/g, firstNames[idx])
        .replace(/Lee/g, lastNames[idx]);
});

const mod_student = entries.map((e, idx) => {
    var num_of_skills = getRandomInt(5) + 1;
    var skill_list = [];
    for (i = 0; i < num_of_skills; i++) {
        skill_list.push(skills[getRandomInt(20)]);
    }
    return STUDENT_TEMPLATE.replace(/idx/g, idx)
        .replace(/william_lee@school.com/g, emails[idx])
        .replace(/address_here/g, addresses[idx])
        .replace(/female/g, genders[idx])
        .replace(/skill_here/g, skill_list.join(', '))
        .replace(/william_lee/g, passwords[idx])
        .replace(/_avatar_/g, snakeCase(emails[idx]) + '.jpg')
        .replace(/William/g, firstNames[idx])
        .replace(/Lee/g, lastNames[idx]);
});

const mod_teacher = entries.map((e, idx) => {
    var num_of_skills = getRandomInt(5) + 1;
    var skill_list = [];
    for (i = 0; i < num_of_skills; i++) {
        skill_list.push(skills[getRandomInt(20)]);
    }
    return TEACHER_TEMPLATE.replace(/idx/g, idx)
        .replace(/william_lee@school.com/g, emails[idx])
        .replace(/address_here/g, addresses[idx])
        .replace(/female/g, genders[idx])
        .replace(/skill_here/g, skill_list.join(', '))
        .replace(/william_lee/g, passwords[idx])
        .replace(/_avatar_/g, snakeCase(emails[idx]) + '.jpg')
        .replace(/William/g, firstNames[idx])
        .replace(/Lee/g, lastNames[idx]);
});

fs.writeFileSync('./write_admin.out', mod_admin.join('\r\r'), { encoding: 'utf-8' });
fs.writeFileSync('./write_teacher.out', mod_teacher.join('\r\r'), { encoding: 'utf-8' });
fs.writeFileSync('./write_student.out', mod_student.join('\r\r'), { encoding: 'utf-8' });
