from dataclasses import dataclass
from typing import Literal
from models.base import BaseModel


@dataclass
class ChildOverride(BaseModel):
    """
    Stores per-child customized points/cost for tasks, penalties, and rewards.
    
    Attributes:
        child_id: ID of the child this override applies to
        entity_id: ID of the task/penalty/reward being customized
        entity_type: Type of entity ('task' or 'reward')
        custom_value: Custom points (for tasks/penalties) or cost (for rewards)
    """
    child_id: str
    entity_id: str
    entity_type: Literal['task', 'reward', 'chore', 'kindness', 'penalty']
    custom_value: int

    def __post_init__(self):
        """Validate custom_value range and entity_type."""
        if self.custom_value < 0 or self.custom_value > 10000:
            raise ValueError("custom_value must be between 0 and 10000")
        if self.entity_type not in ['task', 'reward', 'chore', 'kindness', 'penalty']:
            raise ValueError("entity_type must be 'task', 'reward', 'chore', 'kindness', or 'penalty'")

    @classmethod
    def from_dict(cls, d: dict):
        return cls(
            child_id=d.get('child_id'),
            entity_id=d.get('entity_id'),
            entity_type=d.get('entity_type'),
            custom_value=d.get('custom_value'),
            id=d.get('id'),
            created_at=d.get('created_at'),
            updated_at=d.get('updated_at')
        )

    def to_dict(self):
        base = super().to_dict()
        base.update({
            'child_id': self.child_id,
            'entity_id': self.entity_id,
            'entity_type': self.entity_type,
            'custom_value': self.custom_value
        })
        return base

    @staticmethod
    def create_override(
        child_id: str,
        entity_id: str,
        entity_type: Literal['task', 'reward', 'chore', 'kindness', 'penalty'],
        custom_value: int
    ) -> 'ChildOverride':
        """Factory method to create a new override."""
        return ChildOverride(
            child_id=child_id,
            entity_id=entity_id,
            entity_type=entity_type,
            custom_value=custom_value
        )
