import logging
import sys

from peewee import MySQLDatabase, SqliteDatabase
from storage.s3 import S3Storage
from storage.local import LocalStorage
from data.userfiles import UserRequestFiles
from util import analytics

from test.teststorage import FakeStorage, FakeUserfiles
from test import analytics as fake_analytics


LOG_FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - ' + \
             '%(funcName)s - %(message)s'


class FlaskConfig(object):
  SECRET_KEY = '1cb18882-6d12-440d-a4cc-b7430fb5f884'


class MailConfig(object):
  MAIL_SERVER = 'email-smtp.us-east-1.amazonaws.com'
  MAIL_USE_TLS = True
  MAIL_PORT = 587
  MAIL_USERNAME = 'AKIAIXV5SDGCPVMU3N4Q'
  MAIL_PASSWORD = 'AhmX/vWE91uQ2RtcEKTkfNrzZehEjPNXOXeOXgQNfLao'
  DEFAULT_MAIL_SENDER = 'support@quay.io'
  MAIL_FAIL_SILENTLY = False
  TESTING = False


class SQLiteDB(object):
  DB_NAME = 'test/data/test.db'
  DB_CONNECTION_ARGS = {
    'threadlocals': True
  }
  DB_DRIVER = SqliteDatabase


class EphemeralDB(object):
  DB_NAME = ':memory:'
  DB_CONNECTION_ARGS = {}
  DB_DRIVER = SqliteDatabase


class RDSMySQL(object):
  DB_NAME = 'quay'
  DB_CONNECTION_ARGS = {
    'host': 'fluxmonkeylogin.cb0vumcygprn.us-east-1.rds.amazonaws.com',
    'user': 'fluxmonkey',
    'passwd': '8eifM#uoZ85xqC^',
    'threadlocals': True,
  }
  DB_DRIVER = MySQLDatabase


class AWSCredentials(object):
  AWS_ACCESS_KEY = 'AKIAJWZWUIS24TWSMWRA'
  AWS_SECRET_KEY = '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'
  REGISTRY_S3_BUCKET = 'quay-registry'


class S3Storage(AWSCredentials):
  STORAGE = S3Storage('', AWSCredentials.AWS_ACCESS_KEY,
                      AWSCredentials.AWS_SECRET_KEY,
                      AWSCredentials.REGISTRY_S3_BUCKET)


class LocalStorage(object):
  STORAGE = LocalStorage('test/data/registry')


class FakeStorage(object):
  STORAGE = FakeStorage()


class FakeUserfiles(object):
  USERFILES = FakeUserfiles()


class S3Userfiles(AWSCredentials):
  USERFILES = UserRequestFiles(AWSCredentials.AWS_ACCESS_KEY,
                               AWSCredentials.AWS_SECRET_KEY,
                               AWSCredentials.REGISTRY_S3_BUCKET)


class StripeTestConfig(object):
  STRIPE_SECRET_KEY = 'sk_test_PEbmJCYrLXPW0VRLSnWUiZ7Y'
  STRIPE_PUBLISHABLE_KEY = 'pk_test_uEDHANKm9CHCvVa2DLcipGRh'


class StripeLiveConfig(object):
  STRIPE_SECRET_KEY = 'sk_live_TRuTHYwTvmrLeU3ib7Z9hpqE'
  STRIPE_PUBLISHABLE_KEY = 'pk_live_P5wLU0vGdHnZGyKnXlFG4oiu'


class FakeAnalytics(object):
  ANALYTICS = fake_analytics


class MixpanelTestConfig(object):
  ANALYTICS = analytics
  MIXPANEL_KEY = '38014a0f27e7bdc3ff8cc7cc29c869f9'


class MixpanelProdConfig(MixpanelTestConfig):
  MIXPANEL_KEY = '50ff2b2569faa3a51c8f5724922ffb7e'


class GitHubTestConfig(object):
  GITHUB_CLIENT_ID = 'cfbc4aca88e5c1b40679'
  GITHUB_CLIENT_SECRET = '7d1cc21e17e10cd8168410e2cd1e4561cb854ff9'
  GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token'
  GITHUB_USER_URL = 'https://api.github.com/user'
  GITHUB_USER_EMAILS = GITHUB_USER_URL + '/emails'


class GitHubProdConfig(GitHubTestConfig):
  GITHUB_CLIENT_ID = '5a8c08b06c48d89d4d1e'
  GITHUB_CLIENT_SECRET = 'f89d8bb28ea3bd4e1c68808500d185a816be53b1'


class DigitalOceanConfig(object):
  DO_CLIENT_ID = 'LJ44y2wwYj1MD0BRxS6qHA'
  DO_CLIENT_SECRET = 'b9357a6f6ff45a33bb03f6dbbad135f9'
  DO_SSH_KEY_ID = '46986'
  DO_SSH_PRIVATE_KEY_FILENAME = 'certs/digital_ocean'
  DO_ALLOWED_REGIONS = {1, 4}


class BuildNodeConfig(object):
  BUILD_NODE_PULL_TOKEN = 'F02O2E86CQLKZUQ0O81J8XDHQ6F0N1V36L9JTOEEK6GKKMT1GI8PTJQT4OU88Y6G'


class TestConfig(FlaskConfig, FakeStorage, EphemeralDB, FakeUserfiles,
                 FakeAnalytics):
  LOGGING_CONFIG = {
    'level': logging.DEBUG,
    'format': LOG_FORMAT
  }
  POPULATE_DB_TEST_DATA = True
  TESTING = True
  INCLUDE_TEST_ENDPOINTS = True


class DebugConfig(FlaskConfig, MailConfig, LocalStorage, SQLiteDB,
                  StripeTestConfig, MixpanelTestConfig, GitHubTestConfig,
                  DigitalOceanConfig, BuildNodeConfig, S3Userfiles):
  LOGGING_CONFIG = {
    'level': logging.DEBUG,
    'format': LOG_FORMAT
  }
  SEND_FILE_MAX_AGE_DEFAULT = 0
  POPULATE_DB_TEST_DATA = True
  INCLUDE_TEST_ENDPOINTS = True


class LocalHostedConfig(FlaskConfig, MailConfig, S3Storage, RDSMySQL,
                        StripeLiveConfig, MixpanelTestConfig,
                        GitHubProdConfig, DigitalOceanConfig,
                        BuildNodeConfig, S3Userfiles):
  LOGGING_CONFIG = {
    'level': logging.DEBUG,
    'format': LOG_FORMAT
  }
  SEND_FILE_MAX_AGE_DEFAULT = 0


class ProductionConfig(FlaskConfig, MailConfig, S3Storage, RDSMySQL,
                       StripeLiveConfig, MixpanelProdConfig,
                       GitHubProdConfig, DigitalOceanConfig, BuildNodeConfig,
                       S3Userfiles):
  LOGGING_CONFIG = {
    'stream': sys.stderr,
    'level': logging.DEBUG,
    'format': LOG_FORMAT,
    'filename': 'application.log',
  }
  SEND_FILE_MAX_AGE_DEFAULT = 0
