# database.py
import os
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import init_beanie
from dotenv import load_dotenv

from models import User, Booth, ScanRecord  # 나중에 정의할 모델들

# .env 파일에서 환경 변수 로드
load_dotenv()

# 환경 변수에서 MongoDB URI 가져오기
DATABASE_URI = os.getenv("MONGODB_URI")

if not DATABASE_URI:
    raise ValueError("MONGODB_URI 환경 변수가 설정되지 않았습니다.")

client = AsyncIOMotorClient(DATABASE_URI)
db = client.get_default_database()  # 'event_db'가 기본 데이터베이스로 설정됨

async def init():
    try:
        await init_beanie(database=db, document_models=[User, Booth, ScanRecord])
    
    except Exception as e:
        print(f"Beanie initialization failed: {e}")
        raise e
