# from app.modules.user.user_repository import UserRepository
# from app.modules.user.user_model import UserModel
# import hashlib


# class UserService:
#     def __init__(self):
#         self.repo = UserRepository()

#     async def register_user(self, phone: str,  password: str):

#         # check user already exists
#         existing_user = await self.repo.find_by_phone(phone)
#         if existing_user:
#             return {"error": "User already exists"}

#         # hash password (IMPORTANT)
#         hashed_password = hashlib.sha256(password.encode()).hexdigest()

#         user = UserModel(
#             phone=phone,
#             password=hashed_password
#         )

#         userData = await self.repo.create_user(user)

#         return userData
#     async def get_user_by_phone(self,phone:str):
#         return await self.repo.find_by_phone(phone)
    
#     async def login_user(self, phone: str, password: str):

#         user = await self.repo.find_by_phone(phone)

#         if not user:
#             return {
#                 "success": False,
#                 "message": "User not found"
#             }

#         hashed_password = hashlib.sha256(
#             password.encode()
#         ).hexdigest()

#         if user["password"] != hashed_password:
#             return {
#                 "success": False,
#                 "message": "Invalid password"
#             }

#         return {
#             "success": True,
#             "user": user
#         }



import hashlib

from app.modules.user.user_model import UserModel
from app.modules.user.user_repository import UserRepository


class UserService:

    def __init__(self):
        self.repo = UserRepository()

    def hash_password(
        self,
        password: str
    ) -> str:
        return hashlib.sha256(
            password.encode()
        ).hexdigest()

    async def register_user(
        self,
        phone: str,
        password: str
    ):

        existing_user = await self.repo.find_by_phone(
            phone
        )

        if existing_user:
            return {
                "error": "User already exists"
            }

        hashed_password = self.hash_password(
            password
        )

        user = UserModel(
            phone=phone,
            password=hashed_password
        )

        return await self.repo.create_user(
            user
        )

    async def get_user_by_phone(
        self,
        phone: str
    ):
        return await self.repo.find_by_phone(
            phone
        )
        

    async def login_user(
        self,
        phone: str,
        password: str
    ):

        user = await self.repo.find_by_phone(
            phone
        )

        if not user:
            return {
                "success": False,
                "message": "User not found"
            }

        hashed_password = self.hash_password(
            password
        )

        if user["password"] != hashed_password:
            return {
                "success": False,
                "message": "Invalid password"
            }

        return {
            "success": True,
            "user": user
        }