from pymongo import MongoClient
from config import get_settings
from typing import Optional


class MongoDB:
    client: Optional[MongoClient] = None
    db = None

    @staticmethod
    def connect_db():
        settings = get_settings()
        MongoDB.client = MongoClient(settings.mongodb_uri)
        MongoDB.db = MongoDB.client[settings.mongodb_db_name]
        print(f"✓ Connected to MongoDB: {settings.mongodb_db_name}")

    @staticmethod
    def close_db():
        if MongoDB.client:
            MongoDB.client.close()
            print("✓ Disconnected from MongoDB")

    @staticmethod
    def get_db():
        return MongoDB.db

# Get database instance


def get_database():
    return MongoDB.get_db()
