# python3
# coding=utf-8
# Copyright 2020 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Custom hook for Google Ads.

For Google Ads via the Adwords API details refer to:
https://developers.google.com/adwords/api/docs/reference/release-notes/v201809

A dict with the required tokens for Google Ads Authentication is needed.
See required format here:
https://developers.google.com/adwords/api/docs/guides/first-api-call#get_an_oauth2_refresh_token_and_configure_your_client
"""

import enum
from typing import Any, Dict, List, Mapping, Optional, Text

from airflow.hooks import base_hook
from google.auth import exceptions as google_auth_exceptions
from googleads import adwords
from googleads import common
from googleads import errors as googleads_errors

from plugins.pipeline_plugins.utils import errors


_API_VERSION = 'v201809'
_DEFAULT_BATCH_SIZE = 1000

# Membership lifespan controls how many days that a user's cookie stays on your
# list since its most recent addition to the list. Acceptable range is from 0 to
# 10000, and 10000 means no expiration.
MEMBERSHIP_LIFESPAN_DAYS = 8


class ServiceType(enum.Enum):
  """A enum class for listing available Google Adwords Services.

     For more detail:
     https://developers.google.com/adwords/api/docs/reference/release-notes/v201809
  """
  ADWORDS_USER_LIST_SERVICE = 'AdwordsUserListService'
  OFFLINE_CONVERSION_FEED_SERVICE = 'OfflineConversionFeedService'


class UploadKeyType(enum.Enum):
  """An enum class for listing available upload key types.

  Upload key type is used for how to match the member.
  Available types are:
    1) CONTACT_INFO: Members are matched from customer info such as email
    address, phone number or physical address.
    2) CRM_ID: Members are matched from advertiser generated and assigned user
    ID.
    3) MOBILE_ADVERTISING_ID: Members are matched from mobile advertising IDs.

    For more detail:
    https://developers.google.com/adwords/api/docs/reference/v201809/AdwordsUserListService.CrmBasedUserList
  """
  CONTACT_INFO = enum.auto()
  CRM_ID = enum.auto()
  MOBILE_ADVERTISING_ID = enum.auto()