from dataclasses import dataclass

from clochette.application.repository.CalendarAuthenticationRepository import CalendarAuthenticationRepository
from clochette.application.service.provider.dto.IKeyringService import IKeyringService
from clochette.domain.entity.CalendarID import CalendarID
from clochette.provider.google.dto.GoogleCredentials import GoogleCredentials
from clochette.provider.shared.oauth2.OAuth2Mapper import OAuth2Mapper


@dataclass
class GoogleAuthenticationService(IKeyringService[GoogleCredentials]):
    _calendar_authentication_repository: CalendarAuthenticationRepository
    _oauth2_mapper: OAuth2Mapper

    def store_auth(self, calendar_id: CalendarID, auth: GoogleCredentials) -> None:
        self._calendar_authentication_repository.store_value(
            calendar_id, self._oauth2_mapper.from_oauth2_token(auth.token)
        )

    def retrieve_auth(self, calendar_id: CalendarID) -> GoogleCredentials | None:
        value = self._calendar_authentication_repository.get_value(calendar_id)
        if value:
            return GoogleCredentials(self._oauth2_mapper.to_oauth2_token(value))

        return None
