from dataclasses import dataclass, field
from models.base import BaseModel


@dataclass(kw_only=True)
class RefreshToken(BaseModel):
    user_id: str = ''
    token_hash: str = ''
    token_family: str = ''
    expires_at: str = ''
    is_used: bool = False
    rotated_at: str | None = None

    def to_dict(self):
        return {
            **super().to_dict(),
            'user_id': self.user_id,
            'token_hash': self.token_hash,
            'token_family': self.token_family,
            'expires_at': self.expires_at,
            'is_used': self.is_used,
            'rotated_at': self.rotated_at,
        }

    @staticmethod
    def from_dict(data: dict) -> 'RefreshToken':
        return RefreshToken(
            id=data.get('id', ''),
            created_at=data.get('created_at', 0),
            updated_at=data.get('updated_at', 0),
            user_id=data.get('user_id', ''),
            token_hash=data.get('token_hash', ''),
            token_family=data.get('token_family', ''),
            expires_at=data.get('expires_at', ''),
            is_used=data.get('is_used', False),
            rotated_at=data.get('rotated_at'),
        )
