import base64
from dataclasses import dataclass
from typing import List


@dataclass
class ContextToSync:
    service_key: str
    context_generator: str

    def to_dict(self):
        return {
            "serviceKey": self.service_key,
            "contextGenerator": base64.b64encode(self.context_generator.encode()).decode(),
        }

    @staticmethod
    def from_dict(dct):
        return ContextToSync(
            service_key=dct["serviceKey"],
            context_generator=base64.b64decode(dct["contextGenerator"].encode()).decode(),
        )


@dataclass
class SyncContextRequest:
    contexts: List[ContextToSync]

    def to_dict(self):
        contexts = []
        for context in self.contexts:
            contexts.append(context.to_dict())

        return {
            "contexts": contexts,
        }

    @staticmethod
    def from_dict(dct):
        contexts = []
        for context_dict in dct["contexts"]:
            contexts.append(ContextToSync.from_dict(context_dict))

        return SyncContextRequest(contexts=contexts)
