from dotenv import load_dotenv
import os
import redis

# Load environment variables from .env file
load_dotenv()

# Get the base directory
BASE_DIR = os.path.abspath(os.path.dirname(__file__))

class ApplicationConfig:
    # Secret key for Flask sessions and encryption
    SECRET_KEY = os.environ.get("SECRET_KEY", "default_secret_key")
    
    # Upload folder for datasets (absolute path)
    UPLOAD_FOLDER = os.path.join(BASE_DIR, 'datasets')

    # SQLAlchemy configurations
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ECHO = True

    # Database URI (change localhost to the production database address)
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        "DATABASE_URL", 
        "mysql+pymysql://root:@localhost/CSDashboardDB"
    )

    # Session configurations
    SESSION_TYPE = "redis"
    SESSION_PERMANENT = False
    SESSION_USE_SIGNER = True

    # Redis configuration
    try:
        SESSION_REDIS = redis.from_url(os.environ.get("REDIS_URL", "redis://127.0.0.1:6379"))
    except Exception as e:
        print(f"Redis connection failed: {e}")
        SESSION_REDIS = None
