from pydantic_settings import BaseSettings
from dotenv import load_dotenv
import os
import sys

ENV_FILE = ".env"

if not os.path.exists(ENV_FILE):
    default_env_content = """
APP_NAME=AI Network Configurator
DEBUG=True
API_PREFIX=/api

SILICONFLOW_API_KEY=your-api-key-here
SILICONFLOW_API_URL=https://api.siliconflow.cn/v1

SWITCH_USERNAME=admin
SWITCH_PASSWORD=admin
SWITCH_TIMEOUT=10

ENSP_DEFAULT_IP=172.17.99.201
ENSP_DEFAULT_PORT=2000
"""
    with open(ENV_FILE, "w", encoding="utf-8") as f:
        f.write(default_env_content)

    print(f"已生成默认配置文件 {ENV_FILE} ，请修改后重新运行程序。")
    sys.exit(1)

# 加载 .env 文件
load_dotenv(ENV_FILE)


class Settings(BaseSettings):
    APP_NAME: str
    DEBUG: bool
    API_PREFIX: str

    SILICONFLOW_API_KEY: str
    SILICONFLOW_API_URL: str

    SWITCH_USERNAME: str
    SWITCH_PASSWORD: str
    SWITCH_TIMEOUT: int

    ENSP_DEFAULT_IP: str
    ENSP_DEFAULT_PORT: int

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


settings = Settings()
