from pydantic_settings import BaseSettings
from functools import lru_cache


class Settings(BaseSettings):
    # App
    APP_NAME: str = "Game Marathon"
    DEBUG: bool = False

    # Database
    DATABASE_URL: str = "postgresql+asyncpg://marathon:marathon@localhost:5432/marathon"

    # Security
    SECRET_KEY: str = "your-secret-key-change-in-production"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7  # 7 days

    # OpenAI
    OPENAI_API_KEY: str = ""

    # Telegram
    TELEGRAM_BOT_TOKEN: str = ""
    TELEGRAM_BOT_USERNAME: str = ""
    TELEGRAM_LINK_TOKEN_EXPIRE_MINUTES: int = 10
    BOT_API_SECRET: str = ""  # Secret key for bot-to-backend communication

    # Frontend
    FRONTEND_URL: str = "http://localhost:3000"

    # Uploads
    UPLOAD_DIR: str = "uploads"
    MAX_UPLOAD_SIZE: int = 5 * 1024 * 1024  # 5 MB for avatars
    MAX_IMAGE_SIZE: int = 15 * 1024 * 1024  # 15 MB
    MAX_VIDEO_SIZE: int = 30 * 1024 * 1024  # 30 MB
    ALLOWED_IMAGE_EXTENSIONS: set = {"jpg", "jpeg", "png", "gif", "webp"}
    ALLOWED_VIDEO_EXTENSIONS: set = {"mp4", "webm", "mov"}

    # S3 Storage (FirstVDS)
    S3_ENABLED: bool = False
    S3_BUCKET_NAME: str = ""
    S3_REGION: str = "ru-1"
    S3_ACCESS_KEY_ID: str = ""
    S3_SECRET_ACCESS_KEY: str = ""
    S3_ENDPOINT_URL: str = ""
    S3_PUBLIC_URL: str = ""

    @property
    def ALLOWED_EXTENSIONS(self) -> set:
        return self.ALLOWED_IMAGE_EXTENSIONS | self.ALLOWED_VIDEO_EXTENSIONS

    class Config:
        env_file = ".env"
        extra = "ignore"


@lru_cache
def get_settings() -> Settings:
    return Settings()


settings = get_settings()
