# By: Riasat Ullah
# This file contains functions that validate the information provided to create/edit ITSM entries in the database.

from utils import constants, integration_type_names as intt, var_names
from validations import string_validator
import configuration as configs


def validate_person_data(first_name, last_name, title=None, email=None, iso_code=None, phone=None,
                         person_language=None):
    '''
    Validate the data of an external person.
    :param first_name: first name of the person
    :param last_name: last name of the person
    :param title: title of the person
    :param email: email of the person
    :param iso_code: ISO country code
    :param phone: phone of the person
    :param person_language: language of the person
    '''
    assert string_validator.is_standard_name(first_name)
    assert string_validator.is_standard_name(last_name)

    if title is not None:
        assert isinstance(title, str)
    if email is not None:
        assert string_validator.is_email_address(email)
    if iso_code is not None:
        assert iso_code in list(constants.all_country_codes.keys())
    if phone is not None:
        assert string_validator.is_phone_number(phone)
    if person_language is not None:
        assert person_language in configs.allowed_languages


def validate_group_data(group_name, description, group_type, sla_response, sla_resolution, internal_id):
    '''
    Validate the data of an external group.
    :param group_name: name of the group
    :param description: description of what the group is for
    :param group_type: type of the group
    :param sla_response: (int) minutes by which the first response is expected as per the SLA with the group
    :param sla_resolution: (int) minutes by which the incident should be resolved as per the SLA with the group
    :param internal_id: (str) ID of the group on the organization's internal system
    '''
    assert string_validator.is_standard_name(group_name)
    if description is not None:
        assert isinstance(description, str)
    if group_type is not None:
        assert isinstance(group_type, str)
    if sla_response is not None:
        assert isinstance(sla_response, int)
    if sla_resolution is not None:
        assert isinstance(sla_resolution, int)
    if internal_id is not None:
        assert isinstance(internal_id, str)


def validate_external_sso_settings(organization_id, sso_name, description, integration_type, saml_certificate=None,
                                   saml_key=None, login_url=None, logout_url=None, metadata_url=None, entity_id=None,
                                   vendor_id=None, vendor_subdomain=None, additional_info=None):
    '''
    Validate the SSO settings of an organization
    :param organization_id: ID of the organization
    :param sso_name: name of the SSO settings
    :param description: description of what the SSO setting is for
    :param integration_type: the type of integration
    :param saml_certificate: SAML certificate
    :param saml_key: SAML key
    :param login_url: login url to redirect to
    :param logout_url: logout url to redirect to
    :param metadata_url: SAML metadata url
    :param entity_id: SAML entity ID
    :param vendor_id: ID of the SSO vendor
    :param vendor_subdomain: the subdomain of the organization with the SSO vendor
    :param additional_info: any additional information that might be relevant
    '''
    assert isinstance(organization_id, int)
    assert string_validator.is_standard_name(sso_name)
    if description is not None:
        assert isinstance(description, str)
    assert integration_type in configs.allowed_sso_types
    if saml_certificate is not None:
        assert isinstance(saml_certificate, str)
    if saml_key is not None:
        assert isinstance(saml_key, str)
    if login_url is not None:
        assert string_validator.is_web_url(login_url)
    if logout_url is not None:
        assert string_validator.is_web_url(logout_url)
    if metadata_url is not None:
        assert string_validator.is_web_url(metadata_url)
    if entity_id is not None:
        assert isinstance(entity_id, str)
    if vendor_id is not None:
        assert isinstance(vendor_id, str)
    if vendor_subdomain is not None:
        assert isinstance(vendor_subdomain, str)
    if additional_info is not None:
        assert isinstance(additional_info, dict)
    if integration_type == intt.okta:
        assert var_names.client_id in additional_info and additional_info[var_names.client_id] is not None\
               and not string_validator.is_empty_string(additional_info[var_names.client_id])
        assert var_names.client_id in additional_info and additional_info[var_names.client_secret] is not None\
               and not string_validator.is_empty_string(additional_info[var_names.client_secret])
