import os
import typing

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    VERSION: str = "0.1.0"
    APP_TITLE: str = "Vue FastAPI Admin"
    PROJECT_NAME: str = "Vue FastAPI Admin"
    APP_DESCRIPTION: str = "Description"

    CORS_ORIGINS: typing.List = ["*"]
    CORS_ALLOW_CREDENTIALS: bool = True
    CORS_ALLOW_METHODS: typing.List = ["*"]
    CORS_ALLOW_HEADERS: typing.List = ["*"]

    DEBUG: bool = True

    PROJECT_ROOT: str = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
    BASE_DIR: str = os.path.abspath(os.path.join(PROJECT_ROOT, os.pardir))
    LOGS_ROOT: str = os.path.join(BASE_DIR, "app/logs")
    SECRET_KEY: str = "3488a63e1765035d386f05409663f55c83bfae3b3c61a932744b20ad14244dcf"  # openssl rand -hex 32
    JWT_ALGORITHM: str = "HS256"
    JWT_ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7  # 7 day
    TORTOISE_ORM: dict = {
        "connections": {
            # SQLite configuration
            "sqlite": {
                "engine": "tortoise.backends.sqlite",
                "credentials": {"file_path": f"{BASE_DIR}/db_new.sqlite3"},  # Path to SQLite database file
            },
            # MySQL/MariaDB configuration
            # Install with: tortoise-orm[asyncmy]
            "mysql": {
                "engine": "tortoise.backends.mysql",
                "credentials": {
                    "host": os.getenv("DB_HOST", "localhost"),  # Database host address
                    "port": int(os.getenv("DB_PORT", 3306)),  # Database port
                    "user": os.getenv("DB_USER", "root"),  # Database username
                    "password": os.getenv("DB_PASSWORD", "db_password"),  # Database password
                    "database": os.getenv("DB_NAME", "db_name"),  # Database name
                },
            },
            # PostgreSQL configuration
            # Install with: tortoise-orm[asyncpg]
            # "postgres": {
            #     "engine": "tortoise.backends.asyncpg",
            #     "credentials": {
            #         "host": os.getenv("DB_HOST", "localhost"),  # Database host address
            #         "port": int(os.getenv("DB_PORT", 5432)),  # Database port
            #         "user": os.getenv("DB_USER", "yourusername"),  # Database username
            #         "password": os.getenv("DB_PASSWORD", "yourpassword"),  # Database password
            #         "database": os.getenv("DB_NAME", "yourdatabase"),  # Database name
            #     },
            # },
            # MSSQL/Oracle configuration
            # Install with: tortoise-orm[asyncodbc]
            # "oracle": {
            #     "engine": "tortoise.backends.asyncodbc",
            #     "credentials": {
            #         "host": os.getenv("DB_HOST", "localhost"),  # Database host address
            #         "port": int(os.getenv("DB_PORT", 1433)),  # Database port
            #         "user": os.getenv("DB_USER", "yourusername"),  # Database username
            #         "password": os.getenv("DB_PASSWORD", "yourpassword"),  # Database password
            #         "database": os.getenv("DB_NAME", "yourdatabase"),  # Database name
            #     },
            # },
            # SQLServer configuration
            # Install with: tortoise-orm[asyncodbc]
            # "sqlserver": {
            #     "engine": "tortoise.backends.asyncodbc",
            #     "credentials": {
            #         "host": os.getenv("DB_HOST", "localhost"),  # Database host address
            #         "port": int(os.getenv("DB_PORT", 1433)),  # Database port
            #         "user": os.getenv("DB_USER", "yourusername"),  # Database username
            #         "password": os.getenv("DB_PASSWORD", "yourpassword"),  # Database password
            #         "database": os.getenv("DB_NAME", "yourdatabase"),  # Database name
            #     },
            # },
        },
        "apps": {
            "models": {
                "models": ["app.models", "aerich.models"],
                "default_connection": os.getenv("DB_CONNECTION", "sqlite"),
            },
        },
        "use_tz": False,  # Whether to use timezone-aware datetimes
        "timezone": "Asia/Shanghai",  # Timezone setting
    }
    DATETIME_FORMAT: str = "%Y-%m-%d %H:%M:%S"
    REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379/2")

    LOG_FORMAT: str = "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
    LOG_FILE_MAX_SIZE: int = 1024 * 1024 * 10 # 100 MB
    LOG_FILE_BACKUP_COUNT: int = 5 # 保留5个备份文件


settings = Settings()

TORTOISE_ORM = settings.TORTOISE_ORM

connections = settings.TORTOISE_ORM['connections']
default_connection = settings.TORTOISE_ORM['apps']["models"]["default_connection"]

TORTOISE_ORM['connections'] = {default_connection: connections[default_connection]}
