import os
from dotenv import load_dotenv

load_dotenv()
basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
    SECRET_KEY = os.getenv('SECRET_KEY') or os.urandom(32)
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_RECORD_QUERIES = True
    MAIL_SERVER = os.getenv('MAIL_SERVER') or 'smtp.googlemail.com'
    MAIL_PORT = int(os.environ.get('MAIL_PORT', '587'))
    MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS', 'true').lower() in \
        ['true', 'on', '1']
    MAIL_USERNAME = os.getenv('MAIL_USERNAME')
    MAIL_PASSWORD = os.getenv('MAIL_PASSWORD')
    MUDAWEN_MAIL_SUBJECT_PREFIX = '[Mudawen]'
    MUDAWEN_MAIL_SENDER = 'Mudawen Admin <mudawenapp@gmail.com>'
    MUDAWEN_ADMIN = os.getenv('MUDAWEN_ADMIN')
    MUDAWEN_POSTS_PER_PAGE = 20
    MUDAWEN_FOLLOWERS_PER_PAGE = 50
    MUDAWEN_COMMENTS_PER_PAGE = 30
    MUDAWEN_QUERY_TIME_LIMIT = 0.5

    @staticmethod
    def init_app(app):
        pass


class DevConfig(Config):
    ENV = 'development'
    SQLALCHEMY_DATABASE_URI = os.getenv('DEV_DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')


class TestConfig(Config):
    TESTING = True
    WTF_CSRF_ENABLED = False
    SQLALCHEMY_DATABASE_URI = os.getenv('TEST_DATABASE_URL') or \
        'sqlite://'


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data.sqlite')


config = {
    'development': DevConfig,
    'testing': TestConfig,
    'production': ProductionConfig,
    'default': DevConfig
}
