from pymongo import MongoClient
from src.domain.models import PalmOilBunch

class MongoPalmOilRepository:
    def __init__(self, uri: str, db_name: str, collection_name: str):
        self.client = MongoClient(uri)
        self.collection = self.client[db_name][collection_name]

    def save(self, bunch: PalmOilBunch):
        # Convert dataclass to dict for MongoDB
        doc = bunch.__dict__.copy()
        # Remove id if it's None to let Mongo generate it
        if doc.get('id') is None:
            doc.pop('id')
        
        result = self.collection.insert_one(doc)
        return str(result.inserted_id)
