from drf_spectacular.extensions import OpenApiViewExtension
from drf_spectacular.utils import extend_schema
from rest_framework import serializers

from saas.sso.serializers import UsernameSerializer


class UserInfoSerializer(serializers.Serializer):
    sub = serializers.CharField()
    name = serializers.CharField(required=False)
    email = serializers.EmailField()
    email_verified = serializers.BooleanField()
    preferred_username = serializers.CharField()
    family_name = serializers.CharField(required=False)
    given_name = serializers.CharField(required=False)
    middle_name = serializers.CharField(required=False)
    nickname = serializers.CharField(required=False)
    picture = serializers.URLField(required=False)
    website = serializers.URLField(required=False)
    gender = serializers.CharField(required=False)
    birthdate = serializers.DateField(required=False)
    zoneinfo = serializers.CharField(required=False)
    locale = serializers.CharField(required=False)
    updated_at = serializers.DateTimeField(required=False)


class FixedUserIdentityListEndpoint(OpenApiViewExtension):
    target_class = 'saas.sso.endpoints.identities.UserIdentityListEndpoint'

    def view_replacement(self):
        class UserIdentityListEndpoint(self.target_class):
            @extend_schema(tags=['SSO'], summary='List user identities')
            def get(self, *args, **kwargs):
                pass

        return UserIdentityListEndpoint


class FixedUserIdentityItemEndpoint(OpenApiViewExtension):
    target_class = 'saas.sso.endpoints.identities.UserIdentityItemEndpoint'

    def view_replacement(self):
        class UserIdentityItemEndpoint(self.target_class):
            @extend_schema(tags=['SSO'], summary='Disconnect user identity')
            def delete(self, *args, **kwargs):
                pass

        return UserIdentityItemEndpoint


class FixedSessionUserInfoEndpoint(OpenApiViewExtension):
    target_class = 'saas.sso.endpoints.session.SessionUserInfoEndpoint'

    def view_replacement(self):
        class SessionUserInfoEndpoint(self.target_class):
            serializer_class = UserInfoSerializer

            @extend_schema(tags=['SSO'], summary='Get session user info')
            def get(self, *args, **kwargs):
                pass

        return SessionUserInfoEndpoint


class FixedSessionCreateUserEndpoint(OpenApiViewExtension):
    target_class = 'saas.sso.endpoints.session.SessionCreateUserEndpoint'

    def view_replacement(self):
        class SessionCreateUserEndpoint(self.target_class):
            @extend_schema(tags=['SSO'], summary='Create user from session', request=UsernameSerializer)
            def post(self, *args, **kwargs):
                pass

        return SessionCreateUserEndpoint
