"""Contact resource for ActiveCampaign"""

import typing
from django.http import Http404
from ..base_resource import Resource


class Contact(Resource):
    """
    An ActiveCampaign contact. Allows to:
     - Create a contact
     - Find a contact by email
     - Update a contact
     - Delete a contact

    Check docs in:
    https://developers.activecampaign.com/reference#contact
    """

    def __init__(self, email: str, **kwargs: typing.Dict) -> None:
        """Initialize contact."""
        super().__init__(**kwargs)
        self.email = email

    @staticmethod
    def resource_name() -> str:
        """Get the name of the API resource.

        Returns:
            The name of the resource
        """
        return "contacts"

    @staticmethod
    def map_field_name_to_attribute() -> typing.Dict:
        """Map field names to attributes."""
        return {
            "email": "email",
        }

    @classmethod
    def find(cls: typing.Type, email: str,  exact_match: bool = True) -> "Contact":
        """Gets contact in ActiveCampaign that has the given email. 
        
        The exact_match argument tells whether you like to get 
        results using a search that is not case sensitive and not exact.

        For example, if in ActiveCampaign there are contacts with emails:
            1- '<EMAIL>'
            2- '<EMAIL>'
        
        And we try to find the contact with email='<EMAIL>',
        then when:
            when exact_match=True
                the function returns the 2nd tag
            when exact_match=False
                it could return any of those 2 (don't know why, because the
                2nd one contains the '_' char and the 1rst one doesnt', but
                this was an actual case that were causing inexpected behavior)

        Args:
            - email: 
                The email of the contact to find

            - exact_match: bool
                Whether to return only exact matches. True by default

        Returns:
            The contact with the given email.
        """
        for contact in cls.filter({"email": email}):
            contact_email: str = contact.email
            # Use .casefold() because '<EMAIL>' and '<EMAIL>'
            # are considered as the same email in ActiveCampaign
            if not exact_match or contact_email.casefold() == email.casefold():
                return contact
        raise Http404

"""Contact resource for ActiveCampaign"""

import typing
from django.http import Http404
from ..base_resource import Resource


class Contact(Resource):
    """
    An ActiveCampaign contact. Allows to:
     - Create a contact
     - Find a contact by email
     - Update a contact
     - Delete a contact

    Check docs in:
    https://developers.activecampaign.com/reference#contact
    """

    def __init__(self, email: str, **kwargs: typing.Dict) -> None:
        """Initialize contact."""
        super().__init__(**kwargs)
        self.email = email

    @staticmethod
    def resource_name() -> str:
        """Get the name of the API resource.

        Returns:
            The name of the resource
        """
        return "contacts"

    @staticmethod
    def map_field_name_to_attribute() -> typing.Dict:
        """Map field names to attributes."""
        return {
            "email": "email",
        }

    @classmethod
    def find(cls: typing.Type, email: str,  exact_match: bool = True) -> "Contact":
        """Gets contact in ActiveCampaign that has the given email. 
        
        The exact_match argument tells whether you like to get 
        results using a search that is not case sensitive and not exact.

        For example, if in ActiveCampaign there are contacts with emails:
            1- '<EMAIL>'
            2- '<EMAIL>'
        
        And we try to find the contact with email='<EMAIL>',
        then when:
            when exact_match=True
                the function returns the 2nd tag
            when exact_match=False
                it could return any of those 2 (don't know why, because the
                2nd one contains the '_' char and the 1rst one doesnt', but
                this was an actual case that were causing inexpected behavior)

        Args:
            - email: 
                The email of the contact to find

            - exact_match: bool
                Whether to return only exact matches. True by default

        Returns:
            The contact with the given email.
        """
        for contact in cls.filter({"email": email}):
            contact_email: str = contact.email
            # Use .casefold() because '<EMAIL>' and '<EMAIL>'
            # are considered as the same email in ActiveCampaign
            if not exact_match or contact_email.casefold() == email.casefold():
                return contact
        raise Http404

Contact resource for ActiveCampaign An ActiveCampaign contact. Allows to:      - Create a contact      - Find a contact by email      - Update a contact      - Delete a contact      Check docs in:     https://developers.activecampaign.com/reference#contact Initialize contact. Get the name of the API resource.          Returns:             The name of the resource Map field names to attributes. Gets contact in ActiveCampaign that has the given email.                   The exact_match argument tells whether you like to get          results using a search that is not case sensitive and not exact.          For example, if in ActiveCampaign there are contacts with emails:             1- '<EMAIL>'             2- '<EMAIL>'                  And we try to find the contact with email='<EMAIL>',         then when:             when exact_match=True                 the function returns the 2nd tag             when exact_match=False                 it could return any of those 2 (don't know why, because the                 2nd one contains the '_' char and the 1rst one doesnt', but                 this was an actual case that were causing inexpected behavior)          Args:             - email:                  The email of the contact to find              - exact_match: bool                 Whether to return only exact matches. True by default          Returns:             The contact with the given email. # Use .casefold() because '<EMAIL>' and '<EMAIL>' # are considered as the same email in ActiveCampaign