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('deletion (non-existant)', () => {
    it('delete', async () => {
        const res2 = await axios.delete(base + '/api/organizations/0', axios_config);
        expect(res2.status).toEqual(204);
    });
});
describe('deletion of citizen sould fail', () => {
    it('delete', async () => {
        const res3 = await axios.delete(base + '/api/organizations/1', axios_config);
        expect(res3.status).toEqual(400);
    });
});
// ---------------
describe('adding + deletion (successfull)', () => {
    let added_org_id
    let added_org
    it('creating a new org with just a name should return 200', async () => {
        const res1 = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res1.data
        added_org_id = added_org.id;
        expect(res1.status).toEqual(200);
        expect(res1.headers['content-type']).toContain("application/json")
    });
    it('delete', async () => {
        const res2 = await axios.delete(base + '/api/organizations/' + added_org_id, axios_config);
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
        let added_org2 = res2.data
        added_org_id = added_org2.id;
        delete added_org2.id
        expect(added_org2).toEqual({
            "name": "test123",
            "address": {
                "address1": null,
                "address2": null,
                "city": null,
                "country": null,
                "postalcode": null,
            },
            "registrationEnabled": false,
            "teams": [],
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('check if org really was deleted', async () => {
        const res3 = await axios.get(base + '/api/organizations/' + added_org_id, axios_config);
        expect(res3.status).toEqual(404);
        expect(res3.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('adding + deletion with teams still existing (without force)', () => {
    let added_org;
    let added_org_id;
    let added_team;
    let added_team_id
    it('creating a new org with just a name should return 200', async () => {
        const res1 = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res1.data;
        added_org_id = added_org.id;
        expect(res1.status).toEqual(200);
        expect(res1.headers['content-type']).toContain("application/json")
    });
    it('creating a new team with a valid org should return 200', async () => {
        const res2 = await axios.post(base + '/api/teams', {
            "name": "test123",
            "parentGroup": added_org_id
        }, axios_config);
        added_team = res2.data;
        added_team_id = added_team.id;
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
    });
    it('delete org - this should fail with a 406', async () => {
        const res2 = await axios.delete(base + '/api/organizations/' + added_org_id, axios_config);
        expect(res2.status).toEqual(406);
        expect(res2.headers['content-type']).toContain("application/json")
    });
});
// ---------------
describe('adding + deletion with teams still existing (with force)', () => {
    let added_org;
    let added_org_id;
    let added_team;
    let added_team_id
    it('creating a new org with just a name should return 200', async () => {
        const res1 = await axios.post(base + '/api/organizations', {
            "name": "test123"
        }, axios_config);
        added_org = res1.data;
        added_org_id = added_org.id;
        expect(res1.status).toEqual(200);
        expect(res1.headers['content-type']).toContain("application/json")
    });
    it('creating a new team with a valid org should return 200', async () => {
        const res2 = await axios.post(base + '/api/teams', {
            "name": "test123",
            "parentGroup": added_org_id
        }, axios_config);
        added_team = res2.data;
        added_team_id = added_team.id;
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
    });
    it('delete', async () => {
        const res2 = await axios.delete(base + '/api/organizations/' + added_org_id + '?force=true', axios_config);
        expect(res2.status).toEqual(200);
        expect(res2.headers['content-type']).toContain("application/json")
        let added_org2 = res2.data
        added_org_id = added_org2.id;
        delete added_org2.id;
        delete added_org2.teams;
        expect(added_org2).toEqual({
            "name": "test123",
            "address": {
                "address1": null,
                "address2": null,
                "city": null,
                "country": null,
                "postalcode": null,
            },
            "registrationEnabled": false,
            "responseType": "RUNNERORGANIZATION"
        });
    });
    it('check if org really was deleted', async () => {
        const res3 = await axios.get(base + '/api/organizations/' + added_org_id, axios_config);
        expect(res3.status).toEqual(404);
        expect(res3.headers['content-type']).toContain("application/json")
    });
});