"""StubGen 配置模块"""
from __future__ import annotations
import json
from dataclasses import dataclass, field
from typing import List, Dict, Optional


@dataclass
class StubGenConfig:
    """存根生成器配置"""
    InputDir: Optional[str] = None
    OutputDir: str = "./stubs"
    InputFiles: List[str] = field(default_factory=list)
    IncludePatterns: List[str] = field(default_factory=lambda: ["*.h", "*.c", "*.py"])
    ExcludePatterns: List[str] = field(default_factory=list)
    TypeMappings: Dict[str, str] = field(default_factory=dict)
    PreserveStructure: bool = True  # 保持目录结构
    GenerateGuards: bool = True  # 生成宏守卫
    verbose: bool = False
    DryRun: bool = False

    @classmethod
    def from_dict(cls, data: dict) -> StubGenConfig:
        """从字典创建配置"""
        return cls(
            InputDir=data.get('InputDir'),
            OutputDir=data.get('OutputDir', './stubs'),
            InputFiles=data.get('InputFiles', []),
            IncludePatterns=data.get('IncludePatterns', ['*.h', '*.c', '*.py']),
            ExcludePatterns=data.get('ExcludePatterns', []),
            TypeMappings=data.get('TypeMappings', {}),
            PreserveStructure=data.get('PreserveStructure', True),
            GenerateGuards=data.get('GenerateGuards', True),
            verbose=data.get('verbose', False),
            DryRun=data.get('DryRun', False),
        )

    @classmethod
    def from_file(cls, FilePath: str) -> StubGenConfig:
        """从 JSON 文件加载配置"""
        with open(FilePath, 'r', encoding='utf-8') as f:
            data: dict = json.load(f)
        return cls.from_dict(data)