from dataclasses import dataclass
from models.base import BaseModel


@dataclass
class DigestActionToken(BaseModel):
    user_id: str
    child_id: str
    entity_id: str
    entity_type: str   # 'chore' or 'reward'
    action: str        # 'approve' or 'deny'
    expires_at: str    # ISO timestamp
    used: bool = False
    signature: str = ''

    @classmethod
    def from_dict(cls, d: dict) -> 'DigestActionToken':
        return cls(
            user_id=d.get('user_id'),
            child_id=d.get('child_id'),
            entity_id=d.get('entity_id'),
            entity_type=d.get('entity_type'),
            action=d.get('action'),
            expires_at=d.get('expires_at'),
            used=d.get('used', False),
            signature=d.get('signature', ''),
            id=d.get('id'),
            created_at=d.get('created_at'),
            updated_at=d.get('updated_at'),
        )

    def to_dict(self) -> dict:
        base = super().to_dict()
        base.update({
            'user_id': self.user_id,
            'child_id': self.child_id,
            'entity_id': self.entity_id,
            'entity_type': self.entity_type,
            'action': self.action,
            'expires_at': self.expires_at,
            'used': self.used,
            'signature': self.signature,
        })
        return base
