# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

import boto3
import pyfaaster.aws.tools as faaster_aws_tools
import simplejson as json

from czc.api.environment import TestKeyEnvironment
from czc.constants import PROJECT_NAME

events_client = boto3.client('events')

logger = faaster_aws_tools.setup_logging(PROJECT_NAME)


def publish_event(namespace, detail_type, source, event_detail, event_detail_schema=None):
    validated_event_detail = event_detail_schema(event_detail) if event_detail_schema else event_detail
    entries = [{
        'Source': source,
        'DetailType': detail_type,
        'EventBusName': f'cz-{namespace}-events-primary',
        'Detail': json.dumps(TestKeyEnvironment.add_test_key_parameter(validated_event_detail))
    }]

    logger.debug(f'Sending event bridge entries {entries}')

    response = events_client.put_events(Entries=entries)
    return response['Entries']
