import { faker } from '@faker-js/faker';
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port

let access_token;
let axios_config;

beforeAll(async () => {
    jest.setTimeout(20000);
    const res = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" });
    access_token = res.data["access_token"];
    axios_config = {
        headers: { "authorization": "Bearer " + access_token },
        validateStatus: undefined
    };
});

describe('register invalid citizen', () => {
    it('registering as citizen without mail should return 406', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
        }, axios_config);
        expect(res.status).toEqual(406);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering as citizen with invalid mail should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "email": "user"
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering as citizen without fist name should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "middlename": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering as citizen without last name should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "email": faker.internet.exampleEmail(),
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering as citizen with invalid mail should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "phone": "peter",
            "email": faker.internet.exampleEmail(),
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering as citizen with duplicate mail should return 400', async () => {
        const mail = faker.internet.exampleEmail();
        await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "phone": "peter",
            "email": mail,
        }, axios_config);
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "phone": "peter",
            "email": mail,
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
});
// ---------------
describe('register citizen valid', () => {
    it('registering as citizen with minimal params should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering as citizen with all params should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
            "phone": "+4909132123456",
            "address": {
                address1: "Teststreet 1",
                address2: "Testapartement",
                postalcode: "91074",
                city: "Herzo",
                country: "Germany"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
});
// ---------------
describe('register invalid company', () => {
    let added_org;
    it('creating a new org with just a name and registration enabled should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123",
            "registrationEnabled": true
        }, axios_config);
        added_org = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('registering with bs token should return 404', async () => {
        const res = await axios.post(base + '/api/runners/register/4040404', {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
        }, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering without firstname should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "middlename": "string",
            "lastname": "string",
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering without lastname should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "middlename": "string",
            "firstname": "string",
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering with bs mail should return 400', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "email": "true"
        }, axios_config);
        expect(res.status).toEqual(400);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering with invalid team should return 404', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "lastname": "string",
            "team": 9999999999999999999999
        }, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json");
    });
});
// ---------------
describe('register valid company', () => {
    let added_org;
    let added_team;
    it('creating a new org with just a name and registration enabled should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123",
            "registrationEnabled": true
        }, axios_config);
        added_org = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new team with a parent org should return 200', async () => {
        const res = await axios.post(base + '/api/teams', {
            "name": "test_team",
            "parentGroup": added_org.id
        }, axios_config);
        added_team = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('registering with minimal params should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "lastname": "string",
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering with all params except team should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
            "phone": "+4909132123456",
            "address": {
                address1: "Teststreet 1",
                address2: "Testapartement",
                postalcode: "91074",
                city: "Herzo",
                country: "Germany"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering with minimal params and team should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "lastname": "string",
            "team": added_team.id
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering with all params except team should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
            "phone": "+4909132123456",
            "address": {
                address1: "Teststreet 1",
                address2: "Testapartement",
                postalcode: "91074",
                city: "Herzo",
                country: "Germany"
            }
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('registering with all params and team should return 200', async () => {
        const res = await axios.post(base + '/api/runners/register/' + added_org.registrationKey, {
            "firstname": "string",
            "middlename": "string",
            "lastname": "string",
            "email": faker.internet.exampleEmail(),
            "phone": "+4909132123456",
            "address": {
                address1: "Teststreet 1",
                address2: "Testapartement",
                postalcode: "91074",
                city: "Herzo",
                country: "Germany"
            },
            "team": added_team.id
        }, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
});