from app.modules.otp.otp_model import OtpModel
from app.modules.otp.otp_repository import OtpRepository


class OtpService:

    def __init__(self):
        self.repo = OtpRepository()

    async def add_otp(
        self,
        country_code: str,
        phone: str,
        otpCode: str,
        purpose: str
    ):

        otp = OtpModel(
            country_code=country_code,
            phone=phone,
            otpCode=otpCode,
            purpose=purpose
        )

        otp_id = await self.repo.add_otp(otp)

        return {
            "otp_id": otp_id,
            "country_code": country_code,
            "phone": phone,
            "otpCode": otpCode,
            "purpose": purpose
        }

    async def get_active_otp_by_phone(
        self,
        country_code: str,
        phone: str,
        purpose: str
    ):

        return await self.repo.get_active_otp_by_phone(
            country_code,
            phone,
            purpose
        )

    async def expire_otp(self, _id: str):

        result = await self.repo.expire_otp(_id)

        return {
            "success": True,
            "message": "OTP expired successfully",
            "updated": result
        }

