from fastapi import APIRouter, HTTPException
# Dùng chung ProductDatabaseModel vì nó đã có sẵn hàm get_all_categories
from model.category import CategoryDatabaseModel 
from MongoDBConnection import MONGO_URI, DB_NAME

router = APIRouter()

# Khởi tạo kết nối (Dùng chung cấu trúc với Admin)
db_model = CategoryDatabaseModel(
    db_uri=MONGO_URI,
    db_name=DB_NAME,
    collection_name="categories"
)

@router.get("/categories") # Đường dẫn sẽ là /api/categories theo README
async def get_public_categories():
    try:
        # Gọi hàm lấy tất cả danh mục bạn đã viết bên Admin
        categories = db_model.get_all_categories()
        return categories # Trả về mảng JSON đúng chuẩn README
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))