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

TaskType = Literal['chore', 'kindness', 'penalty']

@dataclass
class Task(BaseModel):
    name: str
    points: int
    type: TaskType
    image_id: str | None = None
    user_id: str | None = None

    @classmethod
    def from_dict(cls, d: dict):
        # Support legacy is_good field for migration
        task_type = d.get('type')
        if task_type is None:
            is_good = d.get('is_good', True)
            task_type = 'chore' if is_good else 'penalty'
        return cls(
            name=d.get('name'),
            points=d.get('points', 0),
            type=task_type,
            image_id=d.get('image_id'),
            user_id=d.get('user_id'),
            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({
            'name': self.name,
            'points': self.points,
            'type': self.type,
            'image_id': self.image_id,
            'user_id': self.user_id
        })
        return base

    @property
    def is_good(self) -> bool:
        """Backward compatibility: chore and kindness are 'good', penalty is not."""
        return self.type != 'penalty'
