import graphene
from graphene_django import DjangoObjectType
from ..models import KYCRequest, KYCRequestRussia


class KYCRequestType(DjangoObjectType):
    class Meta:
        model = KYCRequest
        fields = '__all__'


class KYCRequestRussiaType(DjangoObjectType):
    class Meta:
        model = KYCRequestRussia
        fields = '__all__'


class KYCRequestRussiaInput(graphene.InputObjectType):
    userId = graphene.String(required=True)
    teamUuid = graphene.String(required=True)
    companyName = graphene.String(required=True)
    companyFullName = graphene.String(required=True)
    inn = graphene.String(required=True)
    kpp = graphene.String()
    ogrn = graphene.String()
    address = graphene.String(required=True)
    bankName = graphene.String(required=True)
    bik = graphene.String(required=True)
    correspondentAccount = graphene.String()
    contactPerson = graphene.String(required=True)
    contactEmail = graphene.String(required=True)
    contactPhone = graphene.String(required=True)


class CreateKYCRequestRussia(graphene.Mutation):
    class Arguments:
        input = KYCRequestRussiaInput(required=True)

    kyc_request = graphene.Field(KYCRequestRussiaType)
    success = graphene.Boolean()

    def mutate(self, info, input):
        data = dict(input)

        if 'ogrn' in data:
            data['registration_number'] = data['ogrn']

        data['country_code'] = 'RU'

        field_mapping = {
            'userId': 'user_id',
            'teamUuid': 'team_uuid',
            'companyName': 'company_name',
            'companyFullName': 'company_full_name',
            'bankName': 'bank_name',
            'correspondentAccount': 'correspondent_account',
            'contactPerson': 'contact_person',
            'contactEmail': 'contact_email',
            'contactPhone': 'contact_phone'
        }

        converted_data = {}
        for key, value in data.items():
            db_field = field_mapping.get(key, key)
            converted_data[db_field] = value

        kyc_request = KYCRequestRussia.objects.create(**converted_data)
        return CreateKYCRequestRussia(kyc_request=kyc_request, success=True)


class TeamQuery(graphene.ObjectType):
    """Team schema - Team Access Token authentication"""
    kyc_requests = graphene.List(KYCRequestType)
    kyc_requests_russia = graphene.List(KYCRequestRussiaType)
    kyc_request_russia = graphene.Field(KYCRequestRussiaType, uuid=graphene.String(required=True))

    def resolve_kyc_requests(self, info):
        return KYCRequest.objects.all()

    def resolve_kyc_requests_russia(self, info):
        return KYCRequestRussia.objects.all()

    def resolve_kyc_request_russia(self, info, uuid):
        try:
            return KYCRequestRussia.objects.get(uuid=uuid)
        except KYCRequestRussia.DoesNotExist:
            return None


class TeamMutation(graphene.ObjectType):
    """Team mutations - Team Access Token authentication"""
    create_kyc_request_russia = CreateKYCRequestRussia.Field()


team_schema = graphene.Schema(query=TeamQuery, mutation=TeamMutation)
