/**
 * Copyright (c) 2020 Gitpod GmbH. All rights reserved.
 * Licensed under the GNU Affero General Public License (AGPL).
 * See License-AGPL.txt in the project root for license information.
 */

import { injectable, inject } from "inversify";
import { TypeORM } from "./typeorm";
import { Config } from "../config";


@injectable()
export class DeletedEntryGC {
    @inject(TypeORM) protected readonly typeORM: TypeORM;
    @inject(Config) protected readonly config: Config;

    public start() {
        const cfg = this.config.deletedEntryGCConfig;
        if (!cfg.enabled) {
            console.info("Deleted Entries GC disabled")
            return;
        }

        console.info(`Deleted Entries GC enabled (running every ${cfg.intervalMS/(60*1000)} minutes)`);
        setInterval(() => {
            this.gc().catch(e => console.error("error while removing deleted entries", e));
        }, cfg.intervalMS);
    }

    protected async gc() {
        const conn = await this.typeORM.getConnection();
        await Promise.all(tables.map(t => conn.query(`DELETE FROM ${t.name} WHERE ${t.deletionColumn} = 1`)));
    }

}

const tables: TableWithDeletion[] = [
    { deletionColumn: "deleted", name: "d_b_identity" },
    { deletionColumn: "deleted", name: "d_b_user_storage_resource" },
    { deletionColumn: "deleted", name: "d_b_workspace" },
    { deletionColumn: "deleted", name: "d_b_workspace_instance" },
    { deletionColumn: "deleted", name: "d_b_token_entry" },
    { deletionColumn: "deleted", name: "d_b_gitpod_token" }
];

interface TableWithDeletion {
    name: string;
    deletionColumn: string;
}

