"use strict";

const fs = require('fs'),
  path = require('path'),
  Sequelize = require('sequelize'),
  config = require('config');

function init() {
  const db = {
    sequelize: new Sequelize(config.get("db.games")),
    Sequelize: Sequelize
  };

  return db.sequelize.authenticate().then(function () {
    const Game = db.sequelize.define('game', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      path: Sequelize.STRING,
      name: Sequelize.STRING,
    }, {
      timestamps: false,
      classMethods: {
        associate: function() {
        }
      }
    });

    return db.sequelize.sync({
      force: false
    }).then(function () {
      return db;
    });
  }).catch(function (error) {
    console.log("ERROR: Failed to authenticate with GAMES DB");
    console.log("ERROR: " + JSON.stringify(config.get("db"), null, 2));
    console.log(error);
    throw error;
  });
}

module.exports = init();
