import http from 'k6/http';
import { check, sleep } from 'k6';

const userCreds = [
    { username: 'user1', password: 'password1' },
    { username: 'user2', password: 'password2' },
    { username: 'user3', password: 'password3' },
    { username: 'user4', password: 'password4' },
    { username: 'user5', password: 'password5' },
    { username: 'user6', password: 'password6' },
    { username: 'user7', password: 'password7' },
    { username: 'user8', password: 'password8' },
    { username: 'user9', password: 'password9' },
    { username: 'user10', password: 'password10' },
];

export const options = {
    // 10 VUs, 10 total iterations => each VU does exactly 1 iteration
    vus: 10,
    iterations: 10,
};

export default function () {
    const baseUrl = 'https://iwsv5.rapidworkplace.com/';
    const loginUrl = `${baseUrl}api/Authentication/login`;

    const vuIndex = __VU - 1;

    const { username, password } = userCreds[vuIndex];


    const payload = JSON.stringify({
        userName: username,
        password: password,
    });

    const params = {
        headers: { 'Content-Type': 'application/json' },
    };

    const loginRes = http.post(loginUrl, payload, params);

    check(loginRes, {

        'login: status was 200': (r) => r.status === 200,
    });

    let token;
    try {
        const jsonRes = JSON.parse(loginRes.body);
        token = jsonRes.token;
        //console.log(token)
    } catch (error) {
        console.error('Error parsing login response:', loginRes.body);
    }

    sleep(1);
}
