# Path: amdeb_amazon/model_names/product_operation.py
# PRODUCT_OPERATION_TABLE = 'amdeb.product.operation'
# 
# AMAZON_SYNC_TIMESTAMP_FIELD = 'amazon_sync_timestamp'
# 
# Path: amdeb_amazon/shared/utility.py
# def field_utcnow(*args):
#     """ Return the current UTC day and time in the format expected by the ORM.
#         This function may be used to compute default values.
#     """
#     return datetime.utcnow().strftime(DATETIME_FORMAT)
# 
# Path: amdeb_amazon/models_access/sync_head_access.py
# class SyncHeadAccess(object):
#     """
#     The base class used to define common methods for
#     sync head fields thus each sync_head can use its own access class.
#     """
#     @staticmethod
#     def is_product_template(sync_head):
#         return sync_head[MODEL_NAME_FIELD] == PRODUCT_TEMPLATE_TABLE
# 
#     @staticmethod
#     def is_product_variant(sync_head):
#         return sync_head[MODEL_NAME_FIELD] == PRODUCT_PRODUCT_TABLE
# 
#     @staticmethod
#     def get_write_field_names(sync_head):
#         field_names = sync_head[WRITE_FIELD_NAMES_FIELD]
#         if field_names:
#             data = set(field_names.split(FIELD_NAME_DELIMITER))
#         else:
#             data = set()
#         return data
# 
#     @staticmethod
#     def save_write_field_names(sync_head, write_fields):
#         joined = FIELD_NAME_DELIMITER.join(write_fields)
#         sync_head[WRITE_FIELD_NAMES_FIELD] = joined


import logging
from ..model_names.product_operation import (
    PRODUCT_OPERATION_TABLE,
    AMAZON_SYNC_TIMESTAMP_FIELD,
)
from ..shared.utility import field_utcnow
from .sync_head_access import SyncHeadAccess



_logger = logging.getLogger(__name__)


class ProductOperationAccess(SyncHeadAccess):
    """
    Get new product operations and set operation sync timestamp
    """
    def __init__(self, env):
        self._table = env[PRODUCT_OPERATION_TABLE]

    def search_new_operations(self):
        """
        A new operation doesn't have a sync timestamp
        """
        search_domain = [
            (AMAZON_SYNC_TIMESTAMP_FIELD, '=', False),
        ]
        operations = self._table.search(search_domain)

        log_template = "Found {0} new product operations. Ids: {1}."
        _logger.debug(log_template.format(len(operations), operations.ids))
        return operations

    @staticmethod
    def set_sync_timestamp(operations):
        # set sync timestamp for each operation
        _logger.debug("set sync timestamp for all new product operations.")

# Path: amdeb_amazon/model_names/product_operation.py
# PRODUCT_OPERATION_TABLE = 'amdeb.product.operation'
# 
# AMAZON_SYNC_TIMESTAMP_FIELD = 'amazon_sync_timestamp'
# 
# Path: amdeb_amazon/shared/utility.py
# def field_utcnow(*args):
#     """ Return the current UTC day and time in the format expected by the ORM.
#         This function may be used to compute default values.
#     """
#     return datetime.utcnow().strftime(DATETIME_FORMAT)
# 
# Path: amdeb_amazon/models_access/sync_head_access.py
# class SyncHeadAccess(object):
#     """
#     The base class used to define common methods for
#     sync head fields thus each sync_head can use its own access class.
#     """
#     @staticmethod
#     def is_product_template(sync_head):
#         return sync_head[MODEL_NAME_FIELD] == PRODUCT_TEMPLATE_TABLE
# 
#     @staticmethod
#     def is_product_variant(sync_head):
#         return sync_head[MODEL_NAME_FIELD] == PRODUCT_PRODUCT_TABLE
# 
#     @staticmethod
#     def get_write_field_names(sync_head):
#         field_names = sync_head[WRITE_FIELD_NAMES_FIELD]
#         if field_names:
#             data = set(field_names.split(FIELD_NAME_DELIMITER))
#         else:
#             data = set()
#         return data
# 
#     @staticmethod
#     def save_write_field_names(sync_head, write_fields):
#         joined = FIELD_NAME_DELIMITER.join(write_fields)
#         sync_head[WRITE_FIELD_NAMES_FIELD] = joined


# Path: amdeb_amazon/models_access/product_operation_access.py
import logging
from ..model_names.product_operation import (
    PRODUCT_OPERATION_TABLE,
    AMAZON_SYNC_TIMESTAMP_FIELD,
)
from ..shared.utility import field_utcnow
from .sync_head_access import SyncHeadAccess




_logger = logging.getLogger(__name__)


class ProductOperationAccess(SyncHeadAccess):
    """
    Get new product operations and set operation sync timestamp
    """
    def __init__(self, env):
        self._table = env[PRODUCT_OPERATION_TABLE]

    def search_new_operations(self):
        """
        A new operation doesn't have a sync timestamp
        """
        search_domain = [
            (AMAZON_SYNC_TIMESTAMP_FIELD, '=', False),
        ]
        operations = self._table.search(search_domain)

        log_template = "Found {0} new product operations. Ids: {1}."
        _logger.debug(log_template.format(len(operations), operations.ids))
        return operations

    @staticmethod
    def set_sync_timestamp(operations):
        # set sync timestamp for each operation
        _logger.debug("set sync timestamp for all new product operations.")

        value = {AMAZON_SYNC_TIMESTAMP_FIELD: field_utcnow()}