"""符号表处理的数据结构：四阶段管线中传递的中间数据"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple, TYPE_CHECKING
import ast

if TYPE_CHECKING:
    from lib.core.Handles.HandlesBase import CTypeInfo


@dataclass
class ClassSymbolData:
    """从 AST 提取的类符号数据"""
    name: str
    type_kind: str  # 'struct', 'union', 'enum', 'exception'
    lineno: int
    members: Dict[str, CTypeInfo]
    is_cpython_object: bool = False
    is_packed: bool = False
    enum_members: List[Tuple[str, int, int]] = field(default_factory=list)  # (name, value, lineno)


@dataclass
class TypedefSymbolData:
    """Typedef 符号数据（含原始注解和解析结果）"""
    name: str
    lineno: int
    # 原始注解数据（由 Extractor 设置，Resolver 使用）
    annotation: ast.expr | None = None
    value: ast.expr | int | str | float | None = None
    has_cdefine: bool = False
    has_postdef: bool = False
    has_ctypedef: bool = False
    # 解析结果（由 Resolver 设置，Inserter 使用）
    original_type_kind: Optional[str] = None
    original_class: type | None = None
    members: Optional[Dict] = None


@dataclass
class FuncSymbolData:
    """函数符号数据（含原始注解和解析结果）"""
    name: str
    lineno: int
    # 原始数据（由 Extractor 设置）
    returns_node: ast.expr | None = None
    params: List = field(default_factory=list)  # [(name, annotation_node), ...]
    is_variadic: bool = False
    is_inline: bool = False
    # 解析结果（由 Resolver 设置）
    ret_type: str | None = None
    param_types: List = field(default_factory=list)  # [str, ...]
    # 解析结果（CTypeInfo，保留符号性，由 Resolver 设置）
    ret_ctype: Optional[CTypeInfo] = None


@dataclass
class DefineSymbolData:
    """Define 符号数据（含原始值和解析结果）"""
    name: str
    lineno: int
    # 原始数据（由 Extractor 设置）
    value_node: ast.expr | None = None
    # 解析结果（由 Resolver 设置）
    value: int | str | float | None = None


@dataclass
class AnonymousTypeData:
    """匿名类型数据"""
    name: str
    is_union: bool
    members: Dict[str, CTypeInfo]
    lineno: int


@dataclass
class ModuleSymbols:
    """从模块提取的完整符号数据，在四阶段管线中传递"""
    file_path: str
    classes: List[ClassSymbolData] = field(default_factory=list)
    typedefs: List[TypedefSymbolData] = field(default_factory=list)
    functions: List[FuncSymbolData] = field(default_factory=list)
    defines: List[DefineSymbolData] = field(default_factory=list)
    anonymous_types: Dict[str, AnonymousTypeData] = field(default_factory=dict)
