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

class DataBaseSettings(BaseSettings):
    # 数据库配置
    DB_HOST: str = "10.0.0.17"
    DB_PORT: int = 3306
    DB_USER: str = "root"
    DB_PASSWORD: str = "root"
    DB_NAME: str = "kangda_robotic_dog"
    DB_CHARSET: str = "utf8mb4"
    DB_POOL_SIZE: int = 5
    DB_MAX_OVERFLOW: int = 10
    DB_POOL_TIMEOUT: int = 30
    DB_POOL_RECYCLE: int = 1800

    # class Config:
    #     env_file = ".env"
    
class OCRSettings(BaseException):
    TEXT_DETECTION_MODEL_DIR: str = '/home/admin-root/haotian/康达瑞贝斯机器狗/PaddleOCR-3.1.0/output/PP-OCRv5_server_det_infer_20250814'
    TEXT_RECONGNITION_MODEL_DIR: str = '/home/admin-root/haotian/康达瑞贝斯机器狗/PaddleOCR-3.1.0/output/PP-OCRv5_server_rec_infer_20250815'
    TEXT_DETECTION_MODEL_ONNX_DIR: str ='/home/admin-root/haotian/康达瑞贝斯机器狗/det_shape_20250814.onnx'
    TEXT_RECONGNITION_MODEL_ONNX_DIR: str ='/home/admin-root/haotian/康达瑞贝斯机器狗/rec_shape_20250815.onnx'
    
class YoloV8Settings(BaseException):
    YOLOV8_MODEL_DIR: str = '/home/admin-root/haotian/康达瑞贝斯机器狗/YoloV8Obj/dataset_20250819/train2/weights/best.pt'
    YOLOV8_MODEL_ONNX_DIRS :str='/home/admin-root/haotian/康达瑞贝斯机器狗/yolov8_20250820.onnx'
    

class GetSettings:
    
    def __init__(self):
        self.parse_cli_args()
        
    def get_database_settings(self):
        return DataBaseSettings()
    
    def get_ocr_settings(self):
        return OCRSettings()
    
    def get_yolov8_settings(self):
        return YoloV8Settings()
    
    
    def parse_cli_args(self):
        """
        解析命令行参数
        """
        if 'uvicorn' in sys.argv[0]:
            # 使用uvicorn启动时，命令行参数需要按照uvicorn的文档进行配置，无法自定义参数
            pass
        else:
            # 使用argparse定义命令行参数
            parser = argparse.ArgumentParser(description='命令行参数')
            parser.add_argument('--env', type=str, default='dev', help='运行环境')
            # 解析命令行参数
            args = parser.parse_args()
            # 设置环境变量，如果未设置命令行参数，默认APP_ENV为dev
            os.environ['APP_ENV'] = args.env if args.env else 'dev'
        # 读取运行环境
        run_env = os.environ.get('APP_ENV', '')
        # 运行环境未指定时默认加载.env.dev
        env_file = '.env.dev'
        # 运行环境不为空时按命令行参数加载对应.env文件
        if run_env != '':
            env_file = f'.env.{run_env}'
            
            
        print(f"加载配置 .env.{run_env}")
        # 加载配置
        load_dotenv(env_file)
        
get_settings = GetSettings()

database_settings = get_settings.get_database_settings()

ocr_settings = get_settings.get_ocr_settings()

yolov8_settings = get_settings.get_yolov8_settings()


