# https://docs.authlib.org/en/latest/client/frameworks.html#frameworks-clients
from django.contrib.auth import get_user_model
from django.db import models

from oauth.managers import OAuth2TokenManager


class OAuth2Token(models.Model):
    token_type = models.CharField(max_length=40)
    access_token = models.TextField()
    refresh_token = models.TextField()
    expires_at = models.PositiveIntegerField()
    user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)

    objects = OAuth2TokenManager()

    def to_token(self):
        return dict(
            access_token=self.access_token,
            token_type=self.token_type,
            refresh_token=self.refresh_token,
            expires_at=self.expires_at,
        )
