import os
from dotenv import load_dotenv

load_dotenv()

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'your-secret-key-change-in-production'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///a_plus_academy.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ENGINE_OPTIONS = {
        'connect_args': {'check_same_thread': False}
    }
    SQLALCHEMY_ENGINE_OPTIONS = {
        'connect_args': {
            'check_same_thread': False,
            'timeout': 30,
        },
        'execution_options': {
            "sqlite_wal_mode": True
        }
    }



# Email Configuration - explicit values, not just os.getenv
    MAIL_SERVER = os.environ.get('MAIL_SERVER') or 'smtp.gmail.com'
    MAIL_PORT = int(os.environ.get('MAIL_PORT') or 587)
    MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS', 'True').lower() == 'true'
    MAIL_USE_SSL = False  # Must be False when using TLS on port 587
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER') or MAIL_USERNAME
    
    # Debug to see what's happening
    MAIL_DEBUG = True
    MAIL_SUPPRESS_SEND = False  # Ensure sending isn't suppressed
    
    # Company Settings
    COMPANY_NAME = "A+ Academy"
    COMPANY_EMAIL = os.environ.get('COMPANY_EMAIL') or 'info@aplusacademy.co.za'
    COMPANY_PHONE = os.environ.get('COMPANY_PHONE') or '+27 11 123 4567'
    COMPANY_ADDRESS = os.environ.get('COMPANY_ADDRESS') or '123 Education Street, Johannesburg, South Africa'

    # Admin Credentials
    ADMIN_USERNAME = os.environ.get('ADMIN_USERNAME') or 'admin'
    ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') or 'admin123'
    
    @classmethod
    def print_mail_config(cls):
        print(f"MAIL_SERVER: {cls.MAIL_SERVER}")
        print(f"MAIL_PORT: {cls.MAIL_PORT}")
        print(f"MAIL_USE_TLS: {cls.MAIL_USE_TLS}")
        print(f"MAIL_USERNAME: {cls.MAIL_USERNAME}")
        print(f"MAIL_PASSWORD: {'*' * len(cls.MAIL_PASSWORD) if cls.MAIL_PASSWORD else 'None'}")