"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.disconnectFromDatabase = exports.connectToDatabase = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
// MongoDB connection URL. This should be configured in your environment variables.
const MONGO_URL = process.env.MONGO_URI || "mongodb://oms:oms123.@localhost:27717/omsdb?authSource=admin";
/**
 * Connects to the MongoDB database.
 * This function should be called once at the application's startup.
 * @returns {Promise<void>}
 */
const connectToDatabase = async () => {
    try {
        if (mongoose_1.default.connection.readyState === 1) {
            console.log("[Database] Already connected to MongoDB.");
            return;
        }
        await mongoose_1.default.connect(MONGO_URL);
        console.log("[Database] Successfully connected to MongoDB.");
    }
    catch (error) {
        console.error("[Database] Failed to connect to MongoDB:", error);
        process.exit(1); // Exit process if connection fails
    }
};
exports.connectToDatabase = connectToDatabase;
/**
 * Disconnects from the MongoDB database.
 * This function should be called on application shutdown.
 * @returns {Promise<void>}
 */
const disconnectFromDatabase = async () => {
    try {
        await mongoose_1.default.disconnect();
        console.log("[Database] Disconnected from MongoDB.");
    }
    catch (error) {
        console.error("[Database] Error disconnecting from MongoDB:", error);
    }
};
exports.disconnectFromDatabase = disconnectFromDatabase;
