"""Config for initializing the namex-api."""
import os

from dotenv import find_dotenv, load_dotenv

# this will load all the envars from a .env file located in the project root (api)
load_dotenv(find_dotenv())

CONFIGURATION = {
    'development': 'config.DevConfig',
    'testing': 'config.TestConfig',
    'production': 'config.Config',
    'default': 'config.Config'
}


class Config(object):
    """Base config (also production config)."""

    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

    SECRET_KEY = 'a secret'

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    NRO_SERVICE_ACCOUNT = os.getenv('NRO_SERVICE_ACCOUNT', 'nro_service_account')

    SOLR_BASE_URL = os.getenv('SOLR_BASE_URL', None)
    SOLR_SYNONYMS_API_URL = os.getenv('SOLR_SYNONYMS_API_URL', None)
    NRO_EXTRACTOR_URI = os.getenv('NRO_EXTRACTOR_URI', None)
    AUTO_ANALYZE_URL = os.getenv('AUTO_ANALYZE_URL', None)
    AUTO_ANALYZE_CONFIG = os.getenv('AUTO_ANALYZE_CONFIG', None)
    REPORT_SVC_URL = os.getenv('REPORT_SVC_URL', None)
    REPORT_TEMPLATE_PATH = os.getenv('REPORT_PATH', 'report-templates')

    ALEMBIC_INI = 'migrations/alembic.ini'

    # POSTGRESQL
    DB_USER = os.getenv('DATABASE_USERNAME', '')
    DB_PASSWORD = os.getenv('DATABASE_PASSWORD', '')
    DB_NAME = os.getenv('DATABASE_NAME', '')
    DB_HOST = os.getenv('DATABASE_HOST', '')
    DB_PORT = os.getenv('DATABASE_PORT', '5432')
    SQLALCHEMY_DATABASE_URI = 'postgresql://{user}:{password}@{host}:{port}/{name}'.format(
        user=DB_USER,
        password=DB_PASSWORD,
        host=DB_HOST,
        port=int(DB_PORT),
        name=DB_NAME
    )
    # ORACLE - LEGACY NRO NAMESDB
    NRO_USER = os.getenv('NRO_USER', '')
    NRO_SCHEMA = os.getenv('NRO_SCHEMA', None)
    NRO_PASSWORD = os.getenv('NRO_PASSWORD', '')
    NRO_DB_NAME = os.getenv('NRO_DB_NAME', '')
    NRO_HOST = os.getenv('NRO_HOST', '')
    NRO_PORT = int(os.getenv('NRO_PORT', '1521'))

    # JWT_OIDC Settings
    JWT_OIDC_WELL_KNOWN_CONFIG = os.getenv('JWT_OIDC_WELL_KNOWN_CONFIG')
    JWT_OIDC_ALGORITHMS = os.getenv('JWT_OIDC_ALGORITHMS')
    JWT_OIDC_JWKS_URI = os.getenv('JWT_OIDC_JWKS_URI')
    JWT_OIDC_ISSUER = os.getenv('JWT_OIDC_ISSUER')
    JWT_OIDC_AUDIENCE = os.getenv('JWT_OIDC_AUDIENCE')
    JWT_OIDC_CLIENT_SECRET = os.getenv('JWT_OIDC_CLIENT_SECRET')
    JWT_OIDC_CACHING_ENABLED = os.getenv('JWT_OIDC_CACHING_ENABLED')

    JWT_OIDC_JWKS_CACHE_TIMEOUT = int(os.getenv('JWT_OIDC_JWKS_CACHE_TIMEOUT', '300'))

    TESTING = False,
    DEBUG = False

    # You can disable NRO updates for Name Requests by setting the variable in your .env / OpenShift configuration
    DISABLE_NAMEREQUEST_NRO_UPDATES = int(os.getenv('DISABLE_NAMEREQUEST_NRO_UPDATES', 0))
    DISABLE_NAMEREQUEST_SOLR_UPDATES = int(os.getenv('DISABLE_NAMEREQUEST_SOLR_UPDATES', 0))


class DevConfig(Config):
    """Dev config used for development."""

    TESTING = False,
    DEBUG = True

    # We can't run NRO locally unless you're provisioned, you can disable NRO updates for Name Requests by setting the variable in your .env
    DISABLE_NAMEREQUEST_NRO_UPDATES = int(os.getenv('DISABLE_NAMEREQUEST_NRO_UPDATES', 0))
    DISABLE_NAMEREQUEST_SOLR_UPDATES = int(os.getenv('DISABLE_NAMEREQUEST_SOLR_UPDATES', 0))


class TestConfig(Config):
    """Test config used for pytests."""

    DEBUG = True
    TESTING = True
    # POSTGRESQL
    DB_USER = os.getenv('DATABASE_TEST_USERNAME', '')
    DB_PASSWORD = os.getenv('DATABASE_TEST_PASSWORD', '')
    DB_NAME = os.getenv('DATABASE_TEST_NAME', '')
    DB_HOST = os.getenv('DATABASE_TEST_HOST', '')
    DB_PORT = os.getenv('DATABASE_TEST_PORT', '5432')
    # Allows for NRO add / update bypass if necessary (for local development)
    LOCAL_DEV_MODE = os.getenv('LOCAL_DEV_MODE', False)
    # Set this in your .env to debug SQL Alchemy queries (for local development)
    SQLALCHEMY_ECHO = 'debug' if os.getenv('DEBUG_SQL_QUERIES', False) else False
    SQLALCHEMY_DATABASE_URI = 'postgresql://{user}:{password}@{host}:{port}/{name}'.format(
        user=DB_USER,
        password=DB_PASSWORD,
        host=DB_HOST,
        port=int(DB_PORT),
        name=DB_NAME
    )

    # We can't run NRO locally for running our tests
    DISABLE_NAMEREQUEST_NRO_UPDATES = int(os.getenv('DISABLE_NAMEREQUEST_NRO_UPDATES', 1))
    DISABLE_NAMEREQUEST_SOLR_UPDATES = int(os.getenv('DISABLE_NAMEREQUEST_SOLR_UPDATES', 0))

    # JWT OIDC settings
    # JWT_OIDC_TEST_MODE will set jwt_manager to use
    JWT_OIDC_TEST_MODE = True
    JWT_OIDC_TEST_AUDIENCE = 'example'
    JWT_OIDC_TEST_ISSUER = 'https://example.localdomain/auth/realms/example'
    JWT_OIDC_TEST_KEYS = {
        'keys': [
            {
                'kid': 'flask-jwt-oidc-test-client',
                'kty': 'RSA',
                'alg': 'RS256',
                'use': 'sig',
                'n': 'AN-fWcpCyE5KPzHDjigLaSUVZI0uYrcGcc40InVtl-rQRDmAh-C2W8H4_Hxhr5VLc6crsJ2LiJTV_E72S03pzpOOaaYV6-TzAjCou2GYJIXev7f6Hh512PuG5wyxda_TlBSsI-gvphRTPsKCnPutrbiukCYrnPuWxX5_cES9eStR',  # noqa: E501
                'e': 'AQAB'
            }
        ]
    }

    JWT_OIDC_TEST_PRIVATE_KEY_JWKS = {
        'keys': [
            {
                'kid': 'flask-jwt-oidc-test-client',
                'kty': 'RSA',
                'alg': 'RS256',
                'use': 'sig',
                'n': 'AN-fWcpCyE5KPzHDjigLaSUVZI0uYrcGcc40InVtl-rQRDmAh-C2W8H4_Hxhr5VLc6crsJ2LiJTV_E72S03pzpOOaaYV6-TzAjCou2GYJIXev7f6Hh512PuG5wyxda_TlBSsI-gvphRTPsKCnPutrbiukCYrnPuWxX5_cES9eStR',  # noqa: E501
                'e': 'AQAB',
                'd': 'C0G3QGI6OQ6tvbCNYGCqq043YI_8MiBl7C5dqbGZmx1ewdJBhMNJPStuckhskURaDwk4-8VBW9SlvcfSJJrnZhgFMjOYSSsBtPGBIMIdM5eSKbenCCjO8Tg0BUh_xa3CHST1W4RQ5rFXadZ9AeNtaGcWj2acmXNO3DVETXAX3x0',  # noqa: E501
                'p': 'APXcusFMQNHjh6KVD_hOUIw87lvK13WkDEeeuqAydai9Ig9JKEAAfV94W6Aftka7tGgE7ulg1vo3eJoLWJ1zvKM',
                'q': 'AOjX3OnPJnk0ZFUQBwhduCweRi37I6DAdLTnhDvcPTrrNWuKPg9uGwHjzFCJgKd8KBaDQ0X1rZTZLTqi3peT43s',
                'dp': 'AN9kBoA5o6_Rl9zeqdsIdWFmv4DB5lEqlEnC7HlAP-3oo3jWFO9KQqArQL1V8w2D4aCd0uJULiC9pCP7aTHvBhc',
                'dq': 'ANtbSY6njfpPploQsF9sU26U0s7MsuLljM1E8uml8bVJE1mNsiu9MgpUvg39jEu9BtM2tDD7Y51AAIEmIQex1nM',
                'qi': 'XLE5O360x-MhsdFXx8Vwz4304-MJg-oGSJXCK_ZWYOB_FGXFRTfebxCsSYi0YwJo-oNu96bvZCuMplzRI1liZw'
            }
        ]
    }

    JWT_OIDC_TEST_PRIVATE_KEY_PEM = """
    -----BEGIN RSA PRIVATE KEY-----
    MIICXQIBAAKBgQDfn1nKQshOSj8xw44oC2klFWSNLmK3BnHONCJ1bZfq0EQ5gIfg
    tlvB+Px8Ya+VS3OnK7Cdi4iU1fxO9ktN6c6TjmmmFevk8wIwqLthmCSF3r+3+h4e
    ddj7hucMsXWv05QUrCPoL6YUUz7Cgpz7ra24rpAmK5z7lsV+f3BEvXkrUQIDAQAB
    AoGAC0G3QGI6OQ6tvbCNYGCqq043YI/8MiBl7C5dqbGZmx1ewdJBhMNJPStuckhs
    kURaDwk4+8VBW9SlvcfSJJrnZhgFMjOYSSsBtPGBIMIdM5eSKbenCCjO8Tg0BUh/
    xa3CHST1W4RQ5rFXadZ9AeNtaGcWj2acmXNO3DVETXAX3x0CQQD13LrBTEDR44ei
    lQ/4TlCMPO5bytd1pAxHnrqgMnWovSIPSShAAH1feFugH7ZGu7RoBO7pYNb6N3ia
    C1idc7yjAkEA6Nfc6c8meTRkVRAHCF24LB5GLfsjoMB0tOeEO9w9Ous1a4o+D24b
    AePMUImAp3woFoNDRfWtlNktOqLel5PjewJBAN9kBoA5o6/Rl9zeqdsIdWFmv4DB
    5lEqlEnC7HlAP+3oo3jWFO9KQqArQL1V8w2D4aCd0uJULiC9pCP7aTHvBhcCQQDb
    W0mOp436T6ZaELBfbFNulNLOzLLi5YzNRPLppfG1SRNZjbIrvTIKVL4N/YxLvQbT
    NrQw+2OdQACBJiEHsdZzAkBcsTk7frTH4yGx0VfHxXDPjfTj4wmD6gZIlcIr9lZg
    4H8UZcVFN95vEKxJiLRjAmj6g273pu9kK4ymXNEjWWJn
    -----END RSA PRIVATE KEY-----"""

[((7, 12, 7, 25), 'dotenv.find_dotenv', 'find_dotenv', ({}, {}), '()', False, 'from dotenv import find_dotenv, load_dotenv\n'), ((26, 26, 26, 81), 'os.getenv', 'os.getenv', ({(26, 36, 26, 57): '"""NRO_SERVICE_ACCOUNT"""', (26, 59, 26, 80): '"""nro_service_account"""'}, {}), "('NRO_SERVICE_ACCOUNT', 'nro_service_account')", False, 'import os\n'), ((28, 20, 28, 52), 'os.getenv', 'os.getenv', ({(28, 30, 28, 45): '"""SOLR_BASE_URL"""', (28, 47, 28, 51): 'None'}, {}), "('SOLR_BASE_URL', None)", False, 'import os\n'), ((29, 28, 29, 68), 'os.getenv', 'os.getenv', ({(29, 38, 29, 61): '"""SOLR_SYNONYMS_API_URL"""', (29, 63, 29, 67): 'None'}, {}), "('SOLR_SYNONYMS_API_URL', None)", False, 'import os\n'), ((30, 24, 30, 60), 'os.getenv', 'os.getenv', ({(30, 34, 30, 53): '"""NRO_EXTRACTOR_URI"""', (30, 55, 30, 59): 'None'}, {}), "('NRO_EXTRACTOR_URI', None)", False, 'import os\n'), ((31, 23, 31, 58), 'os.getenv', 'os.getenv', ({(31, 33, 31, 51): '"""AUTO_ANALYZE_URL"""', (31, 53, 31, 57): 'None'}, {}), "('AUTO_ANALYZE_URL', None)", False, 'import os\n'), ((32, 26, 32, 64), 'os.getenv', 'os.getenv', ({(32, 36, 32, 57): '"""AUTO_ANALYZE_CONFIG"""', (32, 59, 32, 63): 'None'}, {}), "('AUTO_ANALYZE_CONFIG', None)", False, 'import os\n'), ((33, 21, 33, 54), 'os.getenv', 'os.getenv', ({(33, 31, 33, 47): '"""REPORT_SVC_URL"""', (33, 49, 33, 53): 'None'}, {}), "('REPORT_SVC_URL', None)", False, 'import os\n'), ((34, 27, 34, 71), 'os.getenv', 'os.getenv', ({(34, 37, 34, 50): '"""REPORT_PATH"""', (34, 52, 34, 70): '"""report-templates"""'}, {}), "('REPORT_PATH', 'report-templates')", False, 'import os\n'), ((39, 14, 39, 48), 'os.getenv', 'os.getenv', ({(39, 24, 39, 43): '"""DATABASE_USERNAME"""', (39, 45, 39, 47): '""""""'}, {}), "('DATABASE_USERNAME', '')", False, 'import os\n'), ((40, 18, 40, 52), 'os.getenv', 'os.getenv', ({(40, 28, 40, 47): '"""DATABASE_PASSWORD"""', (40, 49, 40, 51): '""""""'}, {}), "('DATABASE_PASSWORD', '')", False, 'import os\n'), ((41, 14, 41, 44), 'os.getenv', 'os.getenv', ({(41, 24, 41, 39): '"""DATABASE_NAME"""', (41, 41, 41, 43): '""""""'}, {}), "('DATABASE_NAME', '')", False, 'import os\n'), ((42, 14, 42, 44), 'os.getenv', 'os.getenv', ({(42, 24, 42, 39): '"""DATABASE_HOST"""', (42, 41, 42, 43): '""""""'}, {}), "('DATABASE_HOST', '')", False, 'import os\n'), ((43, 14, 43, 48), 'os.getenv', 'os.getenv', ({(43, 24, 43, 39): '"""DATABASE_PORT"""', (43, 41, 43, 47): '"""5432"""'}, {}), "('DATABASE_PORT', '5432')", False, 'import os\n'), ((52, 15, 52, 40), 'os.getenv', 'os.getenv', ({(52, 25, 52, 35): '"""NRO_USER"""', (52, 37, 52, 39): '""""""'}, {}), "('NRO_USER', '')", False, 'import os\n'), ((53, 17, 53, 46), 'os.getenv', 'os.getenv', ({(53, 27, 53, 39): '"""NRO_SCHEMA"""', (53, 41, 53, 45): 'None'}, {}), "('NRO_SCHEMA', None)", False, 'import os\n'), ((54, 19, 54, 48), 'os.getenv', 'os.getenv', ({(54, 29, 54, 43): '"""NRO_PASSWORD"""', (54, 45, 54, 47): '""""""'}, {}), "('NRO_PASSWORD', '')", False, 'import os\n'), ((55, 18, 55, 46), 'os.getenv', 'os.getenv', ({(55, 28, 55, 41): '"""NRO_DB_NAME"""', (55, 43, 55, 45): '""""""'}, {}), "('NRO_DB_NAME', '')", False, 'import os\n'), ((56, 15, 56, 40), 'os.getenv', 'os.getenv', ({(56, 25, 56, 35): '"""NRO_HOST"""', (56, 37, 56, 39): '""""""'}, {}), "('NRO_HOST', '')", False, 'import os\n'), ((60, 33, 60, 72), 'os.getenv', 'os.getenv', ({(60, 43, 60, 71): '"""JWT_OIDC_WELL_KNOWN_CONFIG"""'}, {}), "('JWT_OIDC_WELL_KNOWN_CONFIG')", False, 'import os\n'), ((61, 26, 61, 58), 'os.getenv', 'os.getenv', ({(61, 36, 61, 57): '"""JWT_OIDC_ALGORITHMS"""'}, {}), "('JWT_OIDC_ALGORITHMS')", False, 'import os\n'), ((62, 24, 62, 54), 'os.getenv', 'os.getenv', ({(62, 34, 62, 53): '"""JWT_OIDC_JWKS_URI"""'}, {}), "('JWT_OIDC_JWKS_URI')", False, 'import os\n'), ((63, 22, 63, 50), 'os.getenv', 'os.getenv', ({(63, 32, 63, 49): '"""JWT_OIDC_ISSUER"""'}, {}), "('JWT_OIDC_ISSUER')", False, 'import os\n'), ((64, 24, 64, 54), 'os.getenv', 'os.getenv', ({(64, 34, 64, 53): '"""JWT_OIDC_AUDIENCE"""'}, {}), "('JWT_OIDC_AUDIENCE')", False, 'import os\n'), ((65, 29, 65, 64), 'os.getenv', 'os.getenv', ({(65, 39, 65, 63): '"""JWT_OIDC_CLIENT_SECRET"""'}, {}), "('JWT_OIDC_CLIENT_SECRET')", False, 'import os\n'), ((66, 31, 66, 68), 'os.getenv', 'os.getenv', ({(66, 41, 66, 67): '"""JWT_OIDC_CACHING_ENABLED"""'}, {}), "('JWT_OIDC_CACHING_ENABLED')", False, 'import os\n'), ((95, 14, 95, 53), 'os.getenv', 'os.getenv', ({(95, 24, 95, 48): '"""DATABASE_TEST_USERNAME"""', (95, 50, 95, 52): '""""""'}, {}), "('DATABASE_TEST_USERNAME', '')", False, 'import os\n'), ((96, 18, 96, 57), 'os.getenv', 'os.getenv', ({(96, 28, 96, 52): '"""DATABASE_TEST_PASSWORD"""', (96, 54, 96, 56): '""""""'}, {}), "('DATABASE_TEST_PASSWORD', '')", False, 'import os\n'), ((97, 14, 97, 49), 'os.getenv', 'os.getenv', ({(97, 24, 97, 44): '"""DATABASE_TEST_NAME"""', (97, 46, 97, 48): '""""""'}, {}), "('DATABASE_TEST_NAME', '')", False, 'import os\n'), ((98, 14, 98, 49), 'os.getenv', 'os.getenv', ({(98, 24, 98, 44): '"""DATABASE_TEST_HOST"""', (98, 46, 98, 48): '""""""'}, {}), "('DATABASE_TEST_HOST', '')", False, 'import os\n'), ((99, 14, 99, 53), 'os.getenv', 'os.getenv', ({(99, 24, 99, 44): '"""DATABASE_TEST_PORT"""', (99, 46, 99, 52): '"""5432"""'}, {}), "('DATABASE_TEST_PORT', '5432')", False, 'import os\n'), ((101, 21, 101, 55), 'os.getenv', 'os.getenv', ({(101, 31, 101, 47): '"""LOCAL_DEV_MODE"""', (101, 49, 101, 54): 'False'}, {}), "('LOCAL_DEV_MODE', False)", False, 'import os\n'), ((20, 35, 20, 60), 'os.path.dirname', 'os.path.dirname', ({(20, 51, 20, 59): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((57, 19, 57, 48), 'os.getenv', 'os.getenv', ({(57, 29, 57, 39): '"""NRO_PORT"""', (57, 41, 57, 47): '"""1521"""'}, {}), "('NRO_PORT', '1521')", False, 'import os\n'), ((68, 38, 68, 85), 'os.getenv', 'os.getenv', ({(68, 48, 68, 77): '"""JWT_OIDC_JWKS_CACHE_TIMEOUT"""', (68, 79, 68, 84): '"""300"""'}, {}), "('JWT_OIDC_JWKS_CACHE_TIMEOUT', '300')", False, 'import os\n'), ((74, 42, 74, 89), 'os.getenv', 'os.getenv', ({(74, 52, 74, 85): '"""DISABLE_NAMEREQUEST_NRO_UPDATES"""', (74, 87, 74, 88): '0'}, {}), "('DISABLE_NAMEREQUEST_NRO_UPDATES', 0)", False, 'import os\n'), ((75, 43, 75, 91), 'os.getenv', 'os.getenv', ({(75, 53, 75, 87): '"""DISABLE_NAMEREQUEST_SOLR_UPDATES"""', (75, 89, 75, 90): '0'}, {}), "('DISABLE_NAMEREQUEST_SOLR_UPDATES', 0)", False, 'import os\n'), ((85, 42, 85, 89), 'os.getenv', 'os.getenv', ({(85, 52, 85, 85): '"""DISABLE_NAMEREQUEST_NRO_UPDATES"""', (85, 87, 85, 88): '0'}, {}), "('DISABLE_NAMEREQUEST_NRO_UPDATES', 0)", False, 'import os\n'), ((86, 43, 86, 91), 'os.getenv', 'os.getenv', ({(86, 53, 86, 87): '"""DISABLE_NAMEREQUEST_SOLR_UPDATES"""', (86, 89, 86, 90): '0'}, {}), "('DISABLE_NAMEREQUEST_SOLR_UPDATES', 0)", False, 'import os\n'), ((103, 33, 103, 70), 'os.getenv', 'os.getenv', ({(103, 43, 103, 62): '"""DEBUG_SQL_QUERIES"""', (103, 64, 103, 69): '(False)'}, {}), "('DEBUG_SQL_QUERIES', False)", False, 'import os\n'), ((113, 42, 113, 89), 'os.getenv', 'os.getenv', ({(113, 52, 113, 85): '"""DISABLE_NAMEREQUEST_NRO_UPDATES"""', (113, 87, 113, 88): '1'}, {}), "('DISABLE_NAMEREQUEST_NRO_UPDATES', 1)", False, 'import os\n'), ((114, 43, 114, 91), 'os.getenv', 'os.getenv', ({(114, 53, 114, 87): '"""DISABLE_NAMEREQUEST_SOLR_UPDATES"""', (114, 89, 114, 90): '0'}, {}), "('DISABLE_NAMEREQUEST_SOLR_UPDATES', 0)", False, 'import os\n')]