from __future__ import annotations

from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.line.views import LineOAuth2Adapter
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class LineAccount(ProviderAccount):
    def get_avatar_url(self):
        return self.account.extra_data.get("pictureUrl") or self.account.extra_data.get(
            "picture"
        )


class LineProvider(OAuth2Provider):
    id = "line"
    name = "Line"
    account_class = LineAccount
    oauth2_adapter_class = LineOAuth2Adapter

    def get_default_scope(self):
        return []

    def extract_uid(self, data):
        return str(data.get("userId") or data.get("sub"))

    def extract_common_fields(self, data):
        return dict(
            email=data.get("email"),
            username=data.get("email") or self.extract_uid(data),
            first_name=data.get("first_name"),
            last_name=data.get("last_name"),
            name=data.get("name"),
        )


provider_classes = [LineProvider]
