#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the check's README for more details.
"""

import argparse  # pylint: disable=C0413
import json  # pylint: disable=C0413
import re  # pylint: disable=C0413
import sys  # pylint: disable=C0413

import lib.args  # pylint: disable=C0413
import lib.base  # pylint: disable=C0413
import lib.human  # pylint: disable=C0413
import lib.infomaniak  # pylint: disable=C0413
import lib.lftest  # pylint: disable=C0413
import lib.time  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
from lib.globals import (STATE_OK, STATE_UNKNOWN,  # pylint: disable=C0413
                          STATE_CRIT, STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025081901'

DESCRIPTION = """Informs you about open events at Infomaniak."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 28


def parse_args():
    """Parse command line arguments using argparse.
    """
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V', '--version',
        action='version',
        version='%(prog)s: v{} by {}'.format(__version__, __author__)
    )

    parser.add_argument(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '--ignore-regex',
        help='Any english title matching this python regex will be ignored '
             '(repeating). '
             'Example: \'(?i)linuxfabrik\' for a case-insensitive search for '
             '"linuxfabrik".',
        action='append',
        default=[],
        dest='IGNORE_REGEX',
    )

    parser.add_argument(
        '--insecure',
        help='This option explicitly allows to perform "insecure" SSL '
             'connections. '
             'Default: %(default)s',
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help='Do not use a proxy. '
             'Default: %(default)s',
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--service',
        help='Only report this service category (repeating). '
             'Example: `--service=swiss_backup --service=public_cloud`. '
             'Default: none (so report all)',
        dest='SERVICE',
        action='append',
        default=None,
    )

    parser.add_argument(
        '--timeout',
        help='Network timeout in seconds. '
             'Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    parser.add_argument(
        '--token',
        help='Infomaniak API token',
        dest='TOKEN',
        required=True,
    )

    parser.add_argument(
        '--test',
        help='For unit tests. '
             'Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".',
        dest='TEST',
        type=lib.args.csv,
    )

    return parser.parse_args()


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # fetch data
    if args.TEST is None:
        result = lib.base.coe(lib.infomaniak.get_events(
            args.TOKEN,
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        ))
    else:
        # do not call the command, put in test data
        stdout, _, _ = lib.lftest.test(args.TEST)
        result = json.loads(stdout)

    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = lib.base.get_perfdata('event', 0, None, None, None, 0, None)
    try:
        compiled_ignore_regex = [re.compile(item) for item in args.IGNORE_REGEX]
    except:
        lib.base.cu('Unable to compile regex.')

    table_data = []

    # analyze data
    for event in result.get('data', {}):
        services = [tr['name'] for tr in event.get('services')]
        # just get the english title of the event message
        title = next((tr['title'] for tr in event.get('description').get('translations') if tr['language'] == 'en'), event.get('description').get('title')) # pylint: disable=C0301

        # a filter for ``--service`` is applied before the ``--ignore-regex`` parameter
        if args.SERVICE and not services:
            # we got a filter for a service category, but an uncategorized event message - skip
            continue
        if args.SERVICE and not set(services).intersection(args.SERVICE):
            # is there any intersection (services wanted)? if not, continue
            continue
        if any(item.search(title) for item in compiled_ignore_regex):
            continue

        services = ', '.join(services)
        if event.get('status') != 'terminated':
            msg += '{}: {} ({}), '.format(
                event.get('type'),
                title,
                services,
            )
            if event.get('type').lower() == 'critical':
                state = STATE_CRIT
            else:
                state = STATE_WARN
            perfdata = perfdata.replace('=0', '=1')

        table_data.append({
            'type': event.get('type'),
            'status': event.get('status'),
            'title': title,
            'services': services,
            'started_at': '{}'.format(
                lib.time.epoch2iso(int(event.get('started_at'))),
            ),
            # in case of an ongoing event, `ended_at` is "None"
            'ended_at': '{} ({} ago)'.format(
                lib.time.epoch2iso(int(event.get('ended_at'))),
                lib.human.seconds2human(lib.time.now(as_type='epoch') - int(event.get('ended_at'))), # pylint: disable=C0301
            ) if event.get('ended_at') is not None else 'in progress',
            'duration': lib.human.seconds2human(
                event.get('ended_at') - event.get('started_at')
            ) if event.get('ended_at') is not None else '',
        })

    # build the message
    if state == STATE_OK:
        msg = 'Everything is ok.\n\n' + msg
    else:
        msg = msg[:-2] + ' - see https://infomaniakstatus.com/en/\n\n'
    if table_data:
        keys = [
            'type',
            'title',
            'services',
            'started_at',
            'ended_at',
            'duration',
        ]
        headers = [
            'Type',
            'Title',
            'Services',
            'Start',
            'End',
            'Duration',
        ]
        msg += lib.base.get_table(table_data, keys, header=headers)

    # over and out
    lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)


if __name__ == '__main__':
    try:
        main()
    except Exception:   # pylint: disable=W0703
        lib.base.cu()
