# from bson import ObjectId
# from app.config.DB import db_manager
# from datetime import datetime
# from app.modules.subscription.subscription_model import SubscriptionModel

# class subscriptionRepository:
#     def __init__(self):
#         self.collection_name = "subscriptionPlan"
#     def get_collection(self):
#         return db_manager.db[self.collection_name]
    
#     async def create_plan(self,subscription:SubscriptionModel):
#         collection = self.get_collection()
#         plan_dict = subscription.model_dump()
#         result = await collection.insert_one(plan_dict)
#         plan_dict["_id"] = str(result.inserted_id)
#         return plan_dict
    
#     async def getAllPlan(self):
#         collection = self.get_collection()
#         result = collection.find({})
#         return await result.to_list(length=None)
    
#     async def getUserPlan(self,userId:str):
#         collection = self.get_collection()
#         return await collection.find_one({
#         "userId": ObjectId(userId)
#     })
#     async def updatePlan(
#     self,
#     plan_id: str,
#     update_data: dict):
#         collection = self.get_collection()

#         result = await collection.update_one(
#         {
#             "_id": ObjectId(plan_id)
#         },
#         {
#             "$set": update_data
#         }
#         )

#         return result
#     async def soft_delete_plan(
#     self,
#     plan_id: str
#     ):
#         collection = self.get_collection()

#         result = await collection.update_one(
#         {
#             "_id": ObjectId(plan_id),
#             "is_deleted": False
#         },
#         {
#             "$set": {
#                 "is_deleted": True,
#                 "deleted_at": datetime.utcnow()
#             }
#         }
#         )

#         return result



from bson import ObjectId
from app.config.DB import db_manager
from datetime import datetime
from app.modules.subscription.subscription_model import SubscriptionModel
from typing import Optional

class SubscriptionRepository:
    def __init__(self):
        self.collection_name = "subscriptionPlan"

    def get_collection(self):
        return db_manager.db[self.collection_name]
    
    async def create_plan(self, subscription: SubscriptionModel) -> dict:
        collection = self.get_collection()
        plan_dict = subscription.model_dump()
        result = await collection.insert_one(plan_dict)
        plan_dict["_id"] = str(result.inserted_id)
        return plan_dict
    
    async def get_all_plans(self) -> list:
        collection = self.get_collection()
        # Sirf wahi plans leke aao jo delete nahi hue hain
        result = collection.find({"is_deleted": {"$ne": True}})
        plans = await result.to_list(length=None)
        
        # MongoDB ObjectId ko frontend ke liye string me convert karo
        for plan in plans:
            plan["_id"] = str(plan["_id"])
        return plans
    
    async def get_plan_by_id(self, plan_id: str) -> Optional[dict]:
        collection = self.get_collection()
        plan = await collection.find_one({"_id": ObjectId(plan_id), "is_deleted": {"$ne": True}})
        if plan:
            plan["_id"] = str(plan["_id"])
        return plan

    async def update_plan(self, plan_id: str, update_data: dict):
        collection = self.get_collection()
        result = await collection.update_one(
            {"_id": ObjectId(plan_id), "is_deleted": {"$ne": True}},
            {"$set": update_data}
        )
        return result

    async def soft_delete_plan(self, plan_id: str):
        collection = self.get_collection()
        result = await collection.update_one(
            {"_id": ObjectId(plan_id), "is_deleted": False},
            {
                "$set": {
                    "is_deleted": True,
                    "deleted_at": datetime.utcnow()
                }
            }
        )
        return result