# Copyright 2018: Mirantis Inc.
# All Rights Reserved.
#
#    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.

import os


RESOLVED_STATUSES = ('UP', 'OK', 'resolved')


def create_file(file_path):
    if not os.path.exists(file_path):
        with open(file_path, 'w+'):
            return file_path


def _format_subject(alert):
    subject = alert['annotations']['summary']
    host = alert['labels'].get('host')
    if host is not None:
        return '[{}] {}'.format(host, subject)
    return subject


def _remove_advice(description):
    return description.split(' Modify the')[0]


def _format_description(alert):
    return _remove_advice(alert['annotations']['description'])


def alert_fields_and_action(alert):
    fields = []

    if alert['status'] in RESOLVED_STATUSES:
        action = 'close_case'
        fields.append(alert['labels'])
    else:
        action = 'create_case'
        # Order of keys matters
        fields.append(_format_subject(alert))
        fields.append(_format_description(alert))
        fields.append(alert['labels'])
    return fields, action
