import json


def mark_for_delete(event):
    """ We created database triggers in our database to write all rows to a polymorphic deleted records table upon hard delete.
    We log the table_name, the time it was deleted, and the row_to_json
    This function creates a new row that looks like a soft delete came from the database

    :param event: A dictionary that includes the Alooma _metadata dictionary
    :return: A dictionary that looks like a soft deleted row
    
    Examples:

    .. code-block:: python

        # Example #1
        event = {'id': 1,
                'table_name': 'alooma_test',
                'primary_key': '123',
                'old_row_json': '{"id":6, "created_at":"2019-01-01"}',
                '_metadata': {'event_type': 'test'}}
        event = mark_for_delete(event)
        event = {'_metadata': {'event_type': 'alooma_test',
                                'table': 'alooma_test'},
                'created_at': '2019-01-01',
                'id': 6,
                'mark_for_delete': True}

    """

    event['_metadata']['event_type'] = event['table_name']
    event['mark_for_delete'] = True

    # Adds a column with the timestamp of when the row was hard deleted
    if 'created_at' in event:
        event['hard_deleted_at'] = event['created_at']
        del event['created_at']

    # Turns the old_row_json into the fields for the normal event
    for k, v in json.loads(event['old_row_json']).items():
        event[k] = v

    event['_metadata']['table'] = event['table_name']
    del event['old_row_json']
    del event['primary_key']
    del event['table_name']

    return event