import { env } from 'process';

import { database, ready, close } from './database';

async function loadTestData() {
    await database('users')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                user_name: 'user0',
                passwd_hash: '$2b$10$sjHhJNz4sLNclKEwWISZRe4cVju6jn4QjMVs4wdZ6wug2SKG774pq',
                email: 'test0@example.com',
                real_name: 'Testing Tester',
                image: null,
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                user_name: 'user1',
                passwd_hash: '$2b$10$sjHhJNz4sLNclKEwWISZRe4cVju6jn4QjMVs4wdZ6wug2SKG774pq',
                email: 'test1@example.com',
                real_name: 'Tester Testing',
                image: Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAIAAAABICAIAAACx52pFAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH5QUXDwcCJWBwEQAADM9JREFUeNrtXGtvHFdyPafu7el58CFasiVT9tr73qyzecDYAM6HIL8+CJDA2C+73l2sE1iWLckUJb5mONN9b518uN0zQ/oFLOIZrT0F8sM02ZyeepyqOlWXwE52spOd7GQnO9nJTnayk53sZCc72cnGhK/iM9EEGEnCXQDF1bP2T6zue+0jsL8OwFUuCPJX2QBxq4qmpFtXSApG0giSDoEgCZAAJaxuESHAiqJ10zZGCuVy+br9Xn/dE36fI6BoHzSB7J9O5WJ5JUCCVK6oUy5JCOwMU34EQt5HQO7MIknagE7/ZiLgy9pn7+XoMIdLW3TeQsLRuTbJYpSiabPOnWTLeySRAfBXSunrEl4h3wdIK2ovF29YhqTZ+i0gEKx4OYr2SbFDL4AysdM7ydvv+A0vv+Hi9xCCuAKZknehgiRGsIP/gvQgJbn72i3Qzc+gLnwoOKWSi/uELUDuLoEsv/lNKL8GdN9fA/SO34EPGAAV7dPYVTnFBlIxQMHxdfdcaqooS6scArkAUVCfvUnlnF8dRIrbVT9oACVnByAASesyKQSxlJMsWjUz0nJOJT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WjJCjDM0ArI2n3bSXeLRjga4rr9UMYphCW52K+jFQlG/c/7yjPbrNUud/3JLpdRZFwd7lz6efdqjVcXlYZv5Lw2XAQbPmETHcsoOOI+760g+feQVaDKpVJVtlbICl5Gfz2/AIkFZKnY9i8gNuq0NTNQ5MbXsN6NQ1gNxhh9AfDvD/8UoYvZmvxujwH4BBY1lvKdBlA2YgWuCJ/uFx8A1Y2uHXQ4wdrANw6F0ZpOS6G+vN4DLqZNlxCyjDrYqSbBpRsnrvEfjvnO+RbqfdfUQPcyBBfU6GWw2Ls9ye6szRyZO9ZJcEdhBndM/t9N5d4YxAB0L/1aNiGxV6BZ9AS3HtxYIns6EYolFGAu0NyqksS6lk0STnndbjn7XqjpISv7ra2hUWvCASJKwqCHUep1cqamakMfwGAQf0xba31cV0XsSTVlq3VcvLotxS99QSAV+x/RdwADK0VnViRRDdOdelLNy//J8HyaOWXom0nO9nJTnayk53sZCc72clOdrKTnexkJzvZyU52spOd7GSz8n9mi2BDTspMpAAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMS0wMy0yMFQxNTo0NzowMyswMDowMI8TNF8AAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTktMDMtMDZUMjE6MjU6NDcrMDA6MDCW2BUnAAAAAElFTkSuQmCC', 'base64'),
            }
        ]);
    await database('teams')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                name: 'Team0',
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                name: 'Team1',
            }, {
                id: '00000000-0000-4000-8000-000000000002',
                name: 'Team2',
            }
        ]);
    await database('roles')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                team_id: '00000000-0000-4000-8000-000000000000',
                name: 'Role0',
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                team_id: '00000000-0000-4000-8000-000000000001',
                name: 'Role1',
            }, {
                id: '00000000-0000-4000-8000-000000000002',
                team_id: '00000000-0000-4000-8000-000000000002',
                name: 'Role2',
            }, {
                id: '00000000-0000-4000-8000-000000000003',
                team_id: '00000000-0000-4000-8000-000000000002',
                name: 'Role3',
            }
        ]);
    await database('team_members')
        .insert([
            {
                user_id: '00000000-0000-4000-8000-000000000000',
                team_id:  '00000000-0000-4000-8000-000000000000',
                role_id:  '00000000-0000-4000-8000-000000000000',
            }, {
                user_id: '00000000-0000-4000-8000-000000000001',
                team_id:  '00000000-0000-4000-8000-000000000001',
                role_id:  '00000000-0000-4000-8000-000000000001',
            }, {
                user_id: '00000000-0000-4000-8000-000000000000',
                team_id:  '00000000-0000-4000-8000-000000000002',
                role_id:  '00000000-0000-4000-8000-000000000002',
            }, {
                user_id: '00000000-0000-4000-8000-000000000001',
                team_id:  '00000000-0000-4000-8000-000000000002',
                role_id:  '00000000-0000-4000-8000-000000000003',
            }
        ]);
    await database('projects')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                name: 'Project0',
                text: 'Project0 Text',
                color: '#00f',
                status: 'open',
                deadline: new Date('2020-10-10'),
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                name: 'Project1',
                text: 'Project1 Text',
                color: '#0f0',
                status: 'closed',
                deadline: null,
            }, {
                id: '00000000-0000-4000-8000-000000000002',
                name: 'Project2',
                text: 'Project2 Text',
                color: '#f00',
                status: 'suspended',
                deadline: null,
            }
        ]);
    await database('team_projects')
        .insert([
            {
                project_id: '00000000-0000-4000-8000-000000000000',
                team_id: '00000000-0000-4000-8000-000000000000',
            }, {
                project_id: '00000000-0000-4000-8000-000000000001',
                team_id: '00000000-0000-4000-8000-000000000002',
            }, {
                project_id: '00000000-0000-4000-8000-000000000002',
                team_id: '00000000-0000-4000-8000-000000000000',
            }, {
                project_id: '00000000-0000-4000-8000-000000000002',
                team_id: '00000000-0000-4000-8000-000000000001',
            }
        ]);
    await database('tasks')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                project_id: '00000000-0000-4000-8000-000000000000',
                name: 'Task0',
                text: 'Task0 Text',
                icon: '0',
                status: 'open',
                priority: 'medium',
                created: new Date('2020-10-05'),
                edited: new Date('2020-10-10'),
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                project_id: '00000000-0000-4000-8000-000000000001',
                name: 'Task1',
                text: 'Task1 Text',
                icon: '1',
                status: 'closed',
                priority: 'high',
                created: new Date('2020-10-10'),
                edited: new Date('2020-10-15'),
            }, {
                id: '00000000-0000-4000-8000-000000000002',
                project_id: '00000000-0000-4000-8000-000000000002',
                name: 'Task2',
                text: 'Task2 Text',
                icon: '2',
                status: 'open',
                priority: 'low',
                created: new Date('2020-10-15'),
                edited: new Date('2020-10-20'),
            }, {
                id: '00000000-0000-4000-8000-000000000003',
                project_id: '00000000-0000-4000-8000-000000000002',
                name: 'Task3',
                text: 'Task3 Text',
                icon: '3',
                status: 'closed',
                priority: 'urgent',
                created: new Date('2020-10-15'),
                edited: new Date('2020-10-20'),
            }, {
                id: '00000000-0000-4000-8000-000000000004',
                project_id: '00000000-0000-4000-8000-000000000002',
                name: 'Task4',
                text: 'Task4 Text',
                icon: '4',
                status: 'suspended',
                priority: 'urgent',
                created: new Date('2020-10-15'),
                edited: new Date('2020-10-20'),
            }, {
                id: '00000000-0000-4000-8000-000000000005',
                project_id: '00000000-0000-4000-8000-000000000002',
                name: 'Task5',
                text: 'Task5 Text',
                icon: '5',
                status: 'open',
                priority: 'urgent',
                created: new Date('2020-10-15'),
                edited: new Date('2020-10-20'),
            }
        ]);
    await database('task_requirements')
        .insert([
            {
                task_id: '00000000-0000-4000-8000-000000000000',
                role_id: '00000000-0000-4000-8000-000000000000',
                time: 30,
            }, {
                task_id: '00000000-0000-4000-8000-000000000001',
                role_id: '00000000-0000-4000-8000-000000000001',
                time: 30,
            }, {
                task_id: '00000000-0000-4000-8000-000000000005',
                role_id: '00000000-0000-4000-8000-000000000000',
                time: 60,
            }, {
                task_id: '00000000-0000-4000-8000-000000000005',
                role_id: '00000000-0000-4000-8000-000000000001',
                time: 30,
            }
        ]);
    await database('task_dependencies')
        .insert([
            {
                task_id: '00000000-0000-4000-8000-000000000002',
                requires_id: '00000000-0000-4000-8000-000000000005',
            }
        ]);
    await database('task_assignees')
        .insert([
            {
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000000',
                time: 120,
                finished: false,
            }
        ]);
    await database('workhours')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000000',
                started: new Date('2020-10-10T00:00:00'),
                finished: new Date('2020-10-10T01:00:00'),
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000000',
                started: new Date('2020-10-10T03:00:00'),
                finished: new Date('2020-10-10T04:00:00'),
            }, {
                id: '00000000-0000-4000-8000-000000000002',
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000000',
                started: new Date('2020-10-10T05:00:00'),
                finished: null,
            }
        ]);
    await database('comments')
        .insert([
            {
                id: '00000000-0000-4000-8000-000000000000',
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000000',
                text: 'Comment0',
                created: new Date('2020-10-10T00:00:00'),
                edited: new Date('2020-10-10T01:00:00'),
            }, {
                id: '00000000-0000-4000-8000-000000000001',
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000001',
                text: 'Comment1',
                created: new Date('2020-10-10T03:00:00'),
                edited: new Date('2020-10-10T04:00:00'),
            }, {
                id: '00000000-0000-4000-8000-000000000002',
                task_id: '00000000-0000-4000-8000-000000000005',
                user_id: '00000000-0000-4000-8000-000000000001',
                text: 'Comment2',
                created: new Date('2020-10-10T05:00:00'),
                edited: new Date('2020-10-10T05:00:00'),
            }
        ]);
}

async function deleteTestData() {
    if (env.NODE_ENV !== 'test') {
        await database('comments')
            .delete()
            .whereIn('comments.task_id', [
                '00000000-0000-4000-8000-000000000005',
            ]);
        await database('workhours')
            .delete()
            .whereIn('workhours.task_id', [
                '00000000-0000-4000-8000-000000000005',
            ]);
        await database('task_assignees')
            .delete()
            .whereIn('task_assignees.task_id', [
                '00000000-0000-4000-8000-000000000005',
            ]);
        await database('task_dependencies')
            .delete()
            .whereIn('task_dependencies.task_id', [
                '00000000-0000-4000-8000-000000000005',
            ]);
        await database('task_requirements')
            .delete()
            .whereIn('task_requirements.task_id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
                '00000000-0000-4000-8000-000000000005',
            ]);
        await database('tasks')
            .delete()
            .whereIn('tasks.id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
                '00000000-0000-4000-8000-000000000002',
                '00000000-0000-4000-8000-000000000003',
                '00000000-0000-4000-8000-000000000004',
                '00000000-0000-4000-8000-000000000005',
            ]);
        await database('team_projects')
            .delete()
            .whereIn('team_projects.team_id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
                '00000000-0000-4000-8000-000000000002',
            ]);
        await database('projects')
            .delete()
            .whereIn('projects.id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
                '00000000-0000-4000-8000-000000000002',
            ]);
        await database('team_members')
            .delete()
            .whereIn('team_members.user_id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
            ]);
        await database('roles')
            .delete()
            .whereIn('roles.id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
                '00000000-0000-4000-8000-000000000002',
                '00000000-0000-4000-8000-000000000003',
            ]);
        await database('teams')
            .delete()
            .whereIn('teams.id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
                '00000000-0000-4000-8000-000000000002',
            ]);
        await database('users')
            .delete()
            .whereIn('users.id', [
                '00000000-0000-4000-8000-000000000000',
                '00000000-0000-4000-8000-000000000001',
            ]);
    } else {
        // The test database is in memory only and does not have to be cleaned
    }
}

beforeAll(async () => {
    await ready;
    await loadTestData();
});

afterAll(async () => {
    await deleteTestData();
    await close();
});