import os
from datetime import timedelta
from dotenv import load_dotenv

# Ensure .env is loaded from the project root
basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, '.env'), override=True)
print(f" * JWT_SECRET_KEY loaded: {'Yes' if os.environ.get('JWT_SECRET_KEY') else 'No'}")
print(f" * GEMINI_API_KEY loaded: {'Yes' if (os.environ.get('GEMINI_API_KEY') or os.environ.get('GOOGLE_API_KEY')) else 'No'}")

class Config:
    # Force both to be identical to ensure signature consistency across all routes
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'qualilearn-secure-shared-key-2026'
    _db_url = os.environ.get('DATABASE_URL')
    if _db_url and _db_url.startswith("postgres://"):
        _db_url = _db_url.replace("postgres://", "postgresql://", 1)
        
    instance_path = os.path.join(basedir, 'instance')
    db_filename = 'qualilearn_v2.db'
    source_db = os.path.join(instance_path, db_filename)
    
    # 1. Determine base path
    if os.environ.get('VERCEL'):
        # On Vercel, we MUST use /tmp for SQLite, but warn that it's ephemeral
        import shutil
        db_path = f'/tmp/{db_filename}'
        if not os.path.exists(db_path) and os.path.exists(source_db):
            try:
                shutil.copy2(source_db, db_path)
            except Exception as e:
                print(f" * Failed to copy DB to /tmp: {e}")
    else:
        # Local or persistent server
        os.makedirs(instance_path, exist_ok=True)
        db_path = source_db
    
    # 2. Construct URI (Favor DATABASE_URL if it's NOT a relative sqlite path)
    if _db_url and not _db_url.startswith("sqlite:///qualilearn_v2.db"):
        SQLALCHEMY_DATABASE_URI = _db_url
    else:
        # Standardize on absolute path for local SQLite to avoid root/instance confusion
        SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.abspath(db_path)

    SQLALCHEMY_TRACK_MODIFICATIONS = False
    JWT_SECRET_KEY = SECRET_KEY 
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=24)
    CORS_HEADERS = 'Content-Type'
    GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY') or os.environ.get('GOOGLE_API_KEY')

class DevelopmentConfig(Config):
    DEBUG = True

class ProductionConfig(Config):
    DEBUG = False

config_by_name = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
}
