import os
from pydantic_settings import BaseSettings
from typing import Optional

class Settings(BaseSettings):
    # 数据库配置
    USE_SQLITE: bool = False  # 设为 False 使用 MySQL
    DB_HOST: str = "127.0.0.1"
    DB_PORT: int = 3306
    DB_USER: str = "cursorpro"
    DB_PASSWORD: str = ""
    DB_NAME: str = "cursorpro"

    # JWT配置
    SECRET_KEY: str = ""  # Must be set via .env
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7  # 7天

    # 管理员账号
    ADMIN_USERNAME: str = "admin"
    ADMIN_PASSWORD: str = ""

    # 外部系统API Token (用于批量上传账号等)
    API_TOKEN: str = ""

    @property
    def DATABASE_URL(self) -> str:
        if self.USE_SQLITE:
            # SQLite 用于本地测试
            db_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "cursorpro.db")
            return f"sqlite:///{db_path}"
        return f"mysql+pymysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}?charset=utf8mb4"

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

settings = Settings()
