"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeDatabase = void 0;
const typeorm_1 = require("typeorm");
const Message_1 = require("./models/Message");
const Run_1 = require("./models/Run");
const RunView_1 = require("./models/RunView");
const Trace_1 = require("./models/Trace");
const InputRequest_1 = require("./models/InputRequest");
const Run_2 = require("./dao/Run");
const InputRequest_2 = require("./dao/InputRequest");
const ModelInvocationView_1 = require("./models/ModelInvocationView");
const FridayApp_1 = require("./models/FridayApp");
const FridayAppView_1 = require("./models/FridayAppView");
const initializeDatabase = (databaseConfig) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const options = Object.assign(Object.assign({}, databaseConfig), { entities: [
                Run_1.RunTable,
                RunView_1.RunView,
                Message_1.MessageTable,
                InputRequest_1.InputRequestTable,
                Trace_1.SpanTable,
                ModelInvocationView_1.ModelInvocationView,
                FridayApp_1.FridayAppMessageTable,
                FridayApp_1.FridayAppReplyTable,
                FridayAppView_1.FridayAppReplyView,
            ], synchronize: true, logging: false });
        const AppDataBase = new typeorm_1.DataSource(options);
        yield AppDataBase.initialize();
        const printingOptions = Object.assign(Object.assign({}, options), { entities: undefined });
        console.log(`Database initialized with options: ${JSON.stringify(printingOptions, null, 2)}`);
        console.log('Refresh the database ...');
        yield Run_2.RunDao.updateRunStatusAtBeginning();
        yield InputRequest_2.InputRequestDao.updateInputRequests();
        console.log('Done');
    }
    catch (error) {
        console.error('Error initializing database', error);
        throw error;
    }
});
exports.initializeDatabase = initializeDatabase;
