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('adding + deletion (non-existant)', () => {
    it('delete', async () => {
        const res = await axios.delete(base + '/api/contacts/0', axios_config);
        expect(res.status).toEqual(204);
    });
});
// ---------------
describe('add+delete (simple)', () => {
    let added_contact;
    it('creating a new contact with only needed params should return 200', async () => {
        const res = await axios.post(base + '/api/contacts', {
            "firstname": "first",
            "lastname": "last"
        }, axios_config);
        added_contact = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('delete contact', async () => {
        const res = await axios.delete(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        let deleted_contact = res.data
        expect(deleted_contact).toEqual(added_contact);
    });
    it('check if contact really was deleted', async () => {
        const res = await axios.get(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('add+delete (with org)', () => {
    let added_org;
    let added_contact;
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        delete res.data.contact;
        delete res.data.teams;
        added_org = res.data
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new contact with a valid org should return 200', async () => {
        const res = await axios.post(base + '/api/contacts', {
            "firstname": "first",
            "lastname": "last",
            "groups": added_org.id
        }, axios_config);
        added_contact = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete contact', async () => {
        const res = await axios.delete(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        delete res.data.groups[0].contact;
        expect(res.data).toEqual(added_contact);
    });
    it('check if contact really was deleted', async () => {
        const res = await axios.get(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('add+delete (with team)', () => {
    let added_org;
    let added_team;
    let added_contact;
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        delete res.data.contact;
        delete res.data.teams;
        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);
        delete res.data.contact;
        delete res.data.parentGroup;
        added_team = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new contact with a valid team should return 200', async () => {
        const res = await axios.post(base + '/api/contacts', {
            "firstname": "first",
            "lastname": "last",
            "groups": added_team.id
        }, axios_config);
        added_contact = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete contact', async () => {
        const res = await axios.delete(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        delete res.data.groups[0].contact;
        expect(res.data).toEqual(added_contact);
    });
    it('check if contact really was deleted', async () => {
        const res = await axios.get(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('add+delete (with org&team)', () => {
    let added_org;
    let added_team;
    let added_contact;
    it('creating a new org with just a name should return 200', async () => {
        const res = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        delete res.data.contact;
        delete res.data.teams;
        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);
        delete res.data.contact;
        delete res.data.parentGroup;
        added_team = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
    });
    it('creating a new contact with a valid org and team should return 200', async () => {
        const res = await axios.post(base + '/api/contacts', {
            "firstname": "first",
            "lastname": "last",
            "groups": [added_org.id, added_team.id]
        }, axios_config);
        added_contact = res.data;
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json");
    });
    it('delete contact', async () => {
        const res = await axios.delete(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(200);
        expect(res.headers['content-type']).toContain("application/json")
        delete res.data.groups[0].contact;
        delete res.data.groups[1].contact;
        expect(res.data).toEqual(added_contact);
    });
    it('check if contact really was deleted', async () => {
        const res = await axios.get(base + '/api/contacts/' + added_contact.id, axios_config);
        expect(res.status).toEqual(404);
        expect(res.headers['content-type']).toContain("application/json")
    });
});