# By: Riasat Ullah
# This file contains all constants and functions related to the N-central integration.

from utils import constants, times

# variables
var_acknowledgement_time = 'AcknowledgementTime'
var_active_notification_trigger_id = 'ActiveNotificationTriggerID'
var_affected_service = 'AffectedService'
var_customer_name = 'CustomerName'
var_device_name = 'DeviceName'
var_device_property = 'DeviceProperty'
var_device_uri = 'DeviceURI'
var_external_customer_id = 'ExternalCustomerID'
var_n_central_uri = 'N-centralURI'
var_notification = 'notification'
var_qualitative_new_state = 'QualitativeNewState'
var_qualitative_old_state = 'QualitativeOldState'
var_task_ident = 'TaskIdent'

# state types
failed_state = 'Failed'
indeterminate_state = 'Indeterminate'
normal_state = 'Normal'
warning_state = 'Warning'


# no data value
no_data_available_in_db = '[no data available in DB]'


def get_tc_mapped_status(new_state: str, ack_time=None):
    '''
    Get the TaskCall equivalent state of an N-central alert.
    :param new_state: (str) new state of the alert
    :param ack_time: (str) ack time -> when ack'ed like 2025-07-09 14:02:31 else: '[no data available in DB]'
    :return: (str) -> OPEN, ACKNOWLEDGED or RESOLVED
    '''
    if new_state.lower() == normal_state.lower():
        return constants.resolved_state
    else:
        if ack_time is not None and ack_time != no_data_available_in_db:
            try:
                times.get_timestamp_from_string(ack_time)
                return constants.acknowledged_state
            except Exception:
                return constants.open_state
        return constants.open_state
