"""诊断系统 — 收集编译过程中的警告和错误

替代原先静默回退的模式：
  - return 'i32'  →  记录 Warning + 回退到 'i32'
  - return []     →  记录 Error + 回退到 []

使用方式:
  diag = DiagnosticCollector()
  diag.warn(file, lineno, "无法解析类型，回退到 i32")
  diag.error(file, lineno, f"加载模块失败: {path}")
  diag.report()  # 打印所有诊断信息
"""
from __future__ import annotations

import os
from dataclasses import dataclass, field
from enum import Enum
from typing import List


class DiagnosticLevel(Enum):
    WARNING = "WARNING"
    ERROR = "ERROR"


@dataclass
class Diagnostic:
    level: DiagnosticLevel
    file: str
    lineno: int
    message: str

    def __str__(self) -> str:
        short_file: str = os.path.basename(self.file) if self.file else "<unknown>"
        return f"[{self.level.value}] {short_file}:{self.lineno} — {self.message}"


class DiagnosticCollector:
    """诊断信息收集器"""

    def __init__(self) -> None:
        self._diagnostics: list[Diagnostic] = []

    def warn(self, file: str, lineno: int, message: str) -> None:
        self._diagnostics.append(Diagnostic(DiagnosticLevel.WARNING, file, lineno, message))

    def error(self, file: str, lineno: int, message: str) -> None:
        self._diagnostics.append(Diagnostic(DiagnosticLevel.ERROR, file, lineno, message))
