#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
模块名称：config.py
模块描述：应用配置管理

作者：czj
创建日期：2026-07-09
最后修改：2026-07-09
"""

import os
from typing import Optional


class Settings:
    """
    应用配置类

    Attributes:
        APP_NAME (str): 应用名称
        APP_VERSION (str): 应用版本
        DEBUG (bool): 是否调试模式
        DATABASE_URL (str): 数据库连接 URL
        CORS_ORIGINS (list): 允许的跨域来源
        PLAYWRIGHT_HEADLESS (bool): Playwright 是否无头模式
        SCREENSHOT_DIR (str): 截图存储目录
    """

    # 基础配置
    APP_NAME: str = "平台自动化测试系统"
    APP_VERSION: str = "1.0.0"
    DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"

    # 数据库配置
    DATABASE_URL: str = os.getenv(
        "DATABASE_URL",
        "sqlite+aiosqlite:///./data/test_platform.db"
    )

    # CORS 配置
    CORS_ORIGINS: list = ["*"]  # 生产环境应限制来源

    # Playwright 配置
    PLAYWRIGHT_HEADLESS: bool = os.getenv(
        "PLAYWRIGHT_HEADLESS",
        "true"
    ).lower() == "true"
    PLAYWRIGHT_TIMEOUT: int = int(os.getenv("PLAYWRIGHT_TIMEOUT", "30000"))

    # 文件存储配置
    SCREENSHOT_DIR: str = os.getenv("SCREENSHOT_DIR", "./data/screenshots")
    REPORT_DIR: str = os.getenv("REPORT_DIR", "./data/reports")

    # 设备模拟配置
    DEVICE_SIM_MAX_INSTANCES: int = int(os.getenv("DEVICE_SIM_MAX_INSTANCES", "50"))
    DEVICE_SIM_DEFAULT_REPORT_INTERVAL: int = int(os.getenv("DEVICE_SIM_DEFAULT_REPORT_INTERVAL", "30"))
    DEVICE_SIM_LOG_RETENTION_DAYS: int = int(os.getenv("DEVICE_SIM_LOG_RETENTION_DAYS", "7"))
    ENCRYPTION_KEY: str = os.getenv("ENCRYPTION_KEY", "")

    # 日志配置
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")

    # Claude 语义增强配置
    CLAUDE_ENABLED: bool = os.getenv("CLAUDE_ENABLED", "true").lower() == "true"
    CLAUDE_TIMEOUT: int = int(os.getenv("CLAUDE_TIMEOUT", "60"))  # 秒（Claude CLI 首次调用需要初始化）
    CLAUDE_MODEL: str = os.getenv("CLAUDE_MODEL", "claude-sonnet-5")  # 默认模型
    CLAUDE_MAX_CANDIDATES: int = int(os.getenv("CLAUDE_MAX_CANDIDATES", "5"))  # 最大候选数

    # ERP 配置
    ERP_API_KEY: str = os.getenv("ERP_API_KEY", "9adc1ce611544fae9bbbc5f6b1d6ce87")
    ERP_BASE_URL: str = os.getenv("ERP_BASE_URL", "https://office.ubainsyun.com:5082/api/uerp")
    ERP_DEVELOPTESTING_ID: int = int(os.getenv("ERP_DEVELOPTESTING_ID", "397"))
    ERP_TYPE_ID: int = int(os.getenv("ERP_TYPE_ID", "2"))
    ERP_IMAGE_URL_PREFIX: str = os.getenv("ERP_IMAGE_URL_PREFIX", "https://office.ubainsyun.com:5015/")

    # 钉钉对接配置
    DINGTALK_APP_KEY: str = os.getenv("DINGTALK_APP_KEY", "")
    DINGTALK_APP_SECRET: str = os.getenv("DINGTALK_APP_SECRET", "")
    DINGTALK_AGENT_ID: int = int(os.getenv("DINGTALK_AGENT_ID", "0"))

    # 被测系统配置
    TARGET_URL: str = os.getenv("TARGET_URL", "https://192.168.5.44")

    # 认证接口路径配置（默认对齐被测系统微前端路径）
    AUTH_LOGIN_PATH: str = os.getenv("AUTH_LOGIN_PATH", "/platform/api/auth/login")
    AUTH_CAPTCHA_PATH: str = os.getenv("AUTH_CAPTCHA_PATH", "/platform/api/code")

    # 语义化用例执行配置（复杂用例通用执行机制，各阶段可独立开关回退）
    # SEMANTIC_ENABLED: 语义目标解析（阶段 3）
    # USE_KEYWORD_MATCHER: legacy 自然语言步骤走 keyword_matcher（阶段 3 过渡路径）
    # USE_FUZZY_MAPPING: 旧模糊评分（阶段 5 关停，默认 false）
    # ENFORCE_VERIFICATION: 强制状态验证（阶段 4，默认 false）
    SEMANTIC_ENABLED: bool = os.getenv("SEMANTIC_ENABLED", "true").lower() == "true"
    USE_KEYWORD_MATCHER: bool = os.getenv("USE_KEYWORD_MATCHER", "false").lower() == "true"
    USE_FUZZY_MAPPING: bool = os.getenv("USE_FUZZY_MAPPING", "false").lower() == "true"
    ENFORCE_VERIFICATION: bool = os.getenv("ENFORCE_VERIFICATION", "false").lower() == "true"

    # 文档管理配置
    DOCUMENT_UPLOAD_DIR: str = os.getenv("DOCUMENT_UPLOAD_DIR", "./data/documents/upload")
    DOCUMENT_OUTPUT_DIR: str = os.getenv("DOCUMENT_OUTPUT_DIR", "./data/documents/output")
    DOCUMENT_MAX_SIZE_MB: int = int(os.getenv("DOCUMENT_MAX_SIZE_MB", "50"))

    class Config:
        """Pydantic 配置"""
        env_file = ".env"


# 全局配置实例
settings = Settings()