# By: Riasat Ullah

import datetime


class AlertLog(object):
    '''
    This class represents an alert log.
    :param timestamp: timestamp when the alert happened
    :param side: side the alert was on; 1 is incoming and 2 is outgoing
    :param event_method: the event method that was user [email, app, etc]
    :param event_type: the type of event [dispatch, acknowledge, etc]
    :param organization_id: ID of the organization the alert is for
    :param user_id: username of the user who the alert was sent to
    :param policy_id: the ID of the policy the alert is for
    :param sender_iso_code: 2 letter ISO code of the sender's phone number
    :param sender_country_code: country code of the sender's phone number
    :param sender_phone_number: the phone number the alert/message was sent from
    :param recipient_iso_code: 2 letter ISO code of the recipient's phone number
    :param recipient_country_code: country code of the recipient's phone number
    :param recipient_phone_number: the phone number the alert/message was sent to
    :param vendor: the vendor who delivered the message [TaskCall, Twilio, etc]
    :param vendor_event_id: the id issued by the vendor to identify the event
    :param instance_id: instance id of the event
    :param attack: (boolean) True if the alert was an attempted attack on the system; False otherwise
    :param attack_type: if there was an attack, the type of attack [SqlInjection, etc]
    :param message: the message that was actually received
    :param processing_succeeded: (boolean) True if the alert was successfully processed
    :param processing_log: the log generated from the processing of the alert
    '''
    def __init__(self, timestamp, side, event_method, event_type=None, organization_id=None, user_id=None,
                 policy_id=None, sender_iso_code=None, sender_country_code=None, sender_phone_number=None,
                 recipient_iso_code=None, recipient_country_code=None, recipient_phone_number=None,
                 vendor=None, vendor_event_id=None, instance_id=None, attack=False, attack_type=None,
                 message=None, processing_succeeded=None, processing_log=None):
        self.timestamp = timestamp
        self.side = side
        self.event_method = event_method
        self.event_type = event_type
        self.organization_id = organization_id
        self.user_id = user_id
        self.policy_id = policy_id
        self.sender_iso_code = sender_iso_code
        self.sender_country_code = sender_country_code
        self.sender_phone_number = sender_phone_number
        self.recipient_iso_code = recipient_iso_code
        self.recipient_country_code = recipient_country_code
        self.recipient_phone_number = recipient_phone_number
        self.vendor = vendor
        self.vendor_event_id = vendor_event_id
        self.instance_id = instance_id
        self.attack = attack
        self.attack_type = attack_type
        self.message = message
        self.processing_succeeded = processing_succeeded
        self.processing_log = processing_log

    def get_db_values(self):
        '''
        Gets the values of an alert log to insert in the database.
        :errors: AssertionError
        '''
        assert isinstance(self.timestamp, datetime.datetime)
        assert self.side in [1, 2]

        values = (
            self.timestamp.date(), self.timestamp, self.side, self.event_method,
            self.event_type, self.organization_id, self.user_id, self.policy_id,
            self.sender_iso_code, self.sender_country_code, self.sender_phone_number, self.recipient_iso_code,
            self.recipient_country_code, self.recipient_phone_number, self.vendor, self.vendor_event_id,
            self.instance_id, self.attack, self.attack_type, self.message,
            self.processing_succeeded, self.processing_log,
        )
        return values
