import uuid
from datetime import (
    datetime,
    timedelta,
)


class Registration(object):
    def __init__(
        self,
        service_name: str,
        service_address: str,
        registration_expiration: float,
    ):
        self.reg_id = str(uuid.uuid4())
        self.service_name = service_name
        self.service_address = service_address
        self.registration_expiration = registration_expiration
        self.last_keep_alive = datetime.now()

    def keep_alive(self):
        self.last_keep_alive = datetime.now()

    def is_expired(self):
        return self.last_keep_alive + timedelta(seconds=self.registration_expiration) < datetime.now()

    def __str__(self):
        return str({
            "reg_id": self.reg_id,
            "service_name": self.service_name,
            "service_address": self.service_address,
            "registration_expiration": self.registration_expiration,
            "last_keep_alive": self.last_keep_alive.isoformat()
        })

    def __repr__(self) -> str:
        return self.__str__()
