import Script from "../Script"
import { OsmPoiDatabase } from "./osmPoiDatabase"

class CreateNewDatabase extends Script {
    constructor() {
        super(
            "Creates a new version of the database. Usage: `createNewDatabase -- YYYY-MM-DD` which will create database `osm-poi.YYYY-MM-DD`"
        )
    }

    async main(args: string[]): Promise<void> {
        const targetName = args[0]
        const overwrite = args[1] === "--overwrite"
        const db = new OsmPoiDatabase("postgresql://user:password@localhost:5444")
        const knownDatabases = await db.findSuitableDatabases()
        if (knownDatabases.indexOf(OsmPoiDatabase.databaseNameFor(targetName)) > 0) {
            if (overwrite) {
                await db.deleteDatabase(targetName)
            } else {
                throw "ERROR: the target database " + targetName + " already exists"
            }
        }
        await db.createNew(targetName)
    }
}

new CreateNewDatabase().run()
