# -*- coding: utf-8 -*-
# Copyright (c) 2017 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 uuid
from decimal import Decimal
from typing import Mapping

from voluptuous import Required, Schema

from czc.date import get_current_timestamp
from czc.schema_validators import UUID


def create_event(schema: Schema, data_fragment: Mapping) -> Mapping:
    return schema({'data': data_fragment})


def create_schema(namespace: str, feature: str, event_name: str, data_schema: Mapping,
                  correlation_id: str = None, version: int = None,
                  timestamp: Decimal = None, event_id: str = None):
    if not correlation_id:
        correlation_id = str(uuid.uuid4())
    if not version:
        version = 1
    if not timestamp:
        timestamp = get_current_timestamp()
    if not event_id:
        event_id = str(uuid.uuid4())
    return Schema({
        Required('correlationId', default=correlation_id): UUID,
        Required('feature', default=feature): str,
        Required('namespace', default=namespace): str,
        Required('eventName', default=event_name): str,
        Required('data'): data_schema,
        Required('id', default=event_id): str,
        Required('timestamp', default=str(timestamp)): str,
        Required('version', default=version): int,
    })
