import os
from pathlib import Path
from dotenv import load_dotenv

load_dotenv()


class Settings:
    APP_ENV: str = os.getenv("APP_ENV", "development")
    SECRET_KEY: str = os.getenv("SECRET_KEY", "dev-secret-key")
    DATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite:///./scholarnet.db")
    UPLOAD_DIR: str = os.getenv("UPLOAD_DIR", "./uploads")
    ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "1440"))
    APP_NAME: str = os.getenv("APP_NAME", "ScholarNet")
    APP_DESCRIPTION: str = os.getenv("APP_DESCRIPTION", "Discover scientific knowledge and stay connected to the world of science")
    APP_URL: str = os.getenv("APP_URL", "http://localhost:8000")
    VERIFIED_EMAIL_DOMAINS: list[str] = [
        d.strip() for d in os.getenv("VERIFIED_EMAIL_DOMAINS", ".edu,.ac.uk").split(",")
    ]
    ALGORITHM: str = "HS256"

    BASE_DIR: Path = Path(__file__).resolve().parent.parent

    def is_email_academic(self, email: str) -> bool:
        domain = email.split("@")[-1].lower()
        return any(domain.endswith(suffix) for suffix in self.VERIFIED_EMAIL_DOMAINS)


settings = Settings()

# Ensure upload directory exists
os.makedirs(settings.UPLOAD_DIR, exist_ok=True)
os.makedirs(os.path.join(settings.UPLOAD_DIR, "publications"), exist_ok=True)
os.makedirs(os.path.join(settings.UPLOAD_DIR, "avatars"), exist_ok=True)
