import json
from typing import (
    Any,
    Dict,
    List,
    Optional,
)


class SaleChallenge(object):

    def __init__(
        self,               # type: SaleChallenge
        name,               # type: str
        start_date,         # type: str
        end_date,           # type: str
        products,           # type: List[int]
        created_by,         # type: int
        challenge_id=None,  # type: Optional[int]
    ):
        # type: (...) -> None

        self.name = name
        self.start_date = start_date
        self.end_date = end_date
        self.products = products
        self.created_by = created_by
        self.challenge_id = challenge_id

    def to_json(
        self,   # type: SaleChallenge
    ):
        # type: (...) -> Dict[str, Any]

        return {
            "name": self.name,
            "start_date": self.start_date,
            "end_date": self.end_date,
            "products": json.dumps(self.products),
            "created_by": self.created_by,
            "challenge_id": self.challenge_id,
        }

    @staticmethod
    def from_json(
        data,   # type: Dict[str, Any]
    ):
        # type: (...) -> SaleChallenge

        return SaleChallenge(
            name=data.get("name"),
            start_date=data.get("start_date"),
            end_date=data.get("end_date"),
            products=json.loads(data.get("products") or "[]"),
            created_by=data.get("created_by"),
            challenge_id=data.get("challenge_id"),
        )
