#!/usr/bin/env python3
"""
导出表模块 - 用于收集和存储文件的公开符号信息
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any


@dataclass
class EnumMember:
    """枚举成员信息"""
    name: str
    value: int
    lineno: int


@dataclass
class EnumInfo:
    """枚举类型信息"""
    name: str
    members: List[EnumMember] = field(default_factory=list)
    lineno: int = 0
    is_public: bool = True


@dataclass
class FunctionParam:
    """函数参数信息"""
    name: str
    type_name: str
    is_pointer: bool = False


@dataclass
class FunctionInfo:
    """函数信息"""
    name: str
    return_type: str
    params: List[FunctionParam] = field(default_factory=list)
    lineno: int = 0
    is_public: bool = True


@dataclass
class StructMember:
    """结构体成员信息"""
    name: str
    type_name: str
    is_pointer: bool = False
    array_size: Optional[int] = None


@dataclass
class StructInfo:
    """结构体类型信息"""
    name: str
    members: List[StructMember] = field(default_factory=list)
    lineno: int = 0
    is_public: bool = True


@dataclass
class TypedefInfo:
    """类型别名信息"""
    name: str
    original_type: str
    lineno: int = 0
    is_public: bool = True


@dataclass
class Exportable:
    """导出表 - 存储文件的所有公开符号"""
    filename: str
    enums: List[EnumInfo] = field(default_factory=list)
    functions: List[FunctionInfo] = field(default_factory=list)
    structs: List[StructInfo] = field(default_factory=list)
    typedefs: List[TypedefInfo] = field(default_factory=list)
    
    def add_enum(self, name: str, lineno: int, is_public: bool = True) -> EnumInfo:
        """添加枚举类型"""
        enum_info: EnumInfo = EnumInfo(name=name, lineno=lineno, is_public=is_public)
        self.enums.append(enum_info)
        return enum_info
    
    def add_function(self, name: str, return_type: str, lineno: int, is_public: bool = True) -> FunctionInfo:
        """添加函数"""
        func_info: FunctionInfo = FunctionInfo(name=name, return_type=return_type, lineno=lineno, is_public=is_public)
        self.functions.append(func_info)
        return func_info
    
    def add_struct(self, name: str, lineno: int, is_public: bool = True) -> StructInfo:
        """添加结构体"""
        struct_info: StructInfo = StructInfo(name=name, lineno=lineno, is_public=is_public)
        self.structs.append(struct_info)
        return struct_info
    
    def add_typedef(self, name: str, original_type: str, lineno: int, is_public: bool = True) -> TypedefInfo:
        """添加类型别名"""
        typedef_info: TypedefInfo = TypedefInfo(name=name, original_type=original_type, lineno=lineno, is_public=is_public)
        self.typedefs.append(typedef_info)
        return typedef_info
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            'filename': self.filename,
            'enums': [
                {
                    'name': enum.name,
                    'lineno': enum.lineno,
                    'is_public': enum.is_public,
                    'members': [
                        {'name': m.name, 'value': m.value, 'lineno': m.lineno}
                        for m in enum.members
                    ]
                }
                for enum in self.enums
            ],
            'functions': [
                {
                    'name': func.name,
                    'return_type': func.return_type,
                    'lineno': func.lineno,
                    'is_public': func.is_public,
                    'params': [
                        {'name': p.name, 'type_name': p.type_name, 'is_pointer': p.is_pointer}
                        for p in func.params
                    ]
                }
                for func in self.functions
            ],
            'structs': [
                {
                    'name': struct.name,
                    'lineno': struct.lineno,
                    'is_public': struct.is_public,
                    'members': [
                        {
                            'name': m.name,
                            'type_name': m.type_name,
                            'is_pointer': m.is_pointer,
                            'array_size': m.array_size
                        }
                        for m in struct.members
                    ]
                }
                for struct in self.structs
            ],
            'typedefs': [
                {
                    'name': typedef.name,
                    'original_type': typedef.original_type,
                    'lineno': typedef.lineno,
                    'is_public': typedef.is_public
                }
                for typedef in self.typedefs
            ]
        }

