from flask_pymongo import PyMongo
from flask import Flask, current_app
from .seed.subject_seed import seed_subjects


def init_db(app: Flask) -> PyMongo:
    try:
        mongo = PyMongo(app)

        mongo.cx.server_info()
        app.logger.info("MongoDB connection established")
        seed_subjects(mongo)
        return mongo
    except Exception as e:
        app.logger.error(f"MongoDB connection failed: {e}")
        return None
