// Declaration model: one variant per construct the generator emits.
// Top-level doc comments and attributes live on `Item`, not inside decl structs.

///|
/// A struct field.
#derive(Debug, Eq)
pub(all) struct StructField {
  name : String
  ty : TypeExpr
  mut_ : Bool
  vis : Visibility
  doc : DocComment
  attrs : Array[Attribute]
}

///|
/// A `struct` declaration: `[vis] struct Name[T] { fields... }`.
#derive(Debug, Eq)
pub(all) struct StructDecl {
  name : String
  type_params : Array[String]
  vis : Visibility
  fields : Array[StructField]
  /// Trait names for `#derive(...)`; synthesized by the renderer.
  derives : Array[String]
}

///|
/// An enum (or suberror) variant: unit when `payload` is empty, otherwise one
/// labelled or positional argument per entry.
#derive(Debug, Eq)
pub(all) struct EnumVariant {
  name : String
  payload : Array[(String?, TypeExpr)]
  doc : DocComment
}

///|
/// An `enum` declaration: `[vis] enum Name[T] { variants... }`.
#derive(Debug, Eq)
pub(all) struct EnumDecl {
  name : String
  type_params : Array[String]
  vis : Visibility
  variants : Array[EnumVariant]
  /// Trait names for `#derive(...)`; synthesized by the renderer.
  derives : Array[String]
  /// Emit `#non_exhaustive` (callers must match with `Type::..`).
  non_exhaustive : Bool
}

///|
/// `suberror Name { ... }` (or unit `suberror Name` when `variants` is empty).
#derive(Debug, Eq)
pub(all) struct SuberrorDecl {
  name : String
  vis : Visibility
  variants : Array[EnumVariant]
  /// Trait names for `#derive(...)`; synthesized by the renderer.
  derives : Array[String]
}

///|
/// Tuple struct / newtype: `pub(all) struct Id(String)`.
#derive(Debug, Eq)
pub(all) struct NewtypeDecl {
  name : String
  type_params : Array[String]
  vis : Visibility
  fields : Array[TypeExpr]
  /// Trait names for `#derive(...)`; synthesized by the renderer.
  derives : Array[String]
}

///|
/// Abstract type: `pub type Handle`.
#derive(Debug, Eq)
pub(all) struct AbstractTypeDecl {
  name : String
  type_params : Array[String]
  vis : Visibility
}

///|
/// A type alias declaration: `[vis] typealias Target as Name` / `pub type Name = Target`.
#derive(Debug, Eq)
pub(all) struct AliasDecl {
  name : String
  type_params : Array[String]
  target : TypeExpr
  vis : Visibility
}

///|
/// Parameter passing style.
///
/// - `Positional` — `name : T`
/// - `DiscardPositional` — `_ : T`
/// - `Labelled` — `name~ : T`
/// - `QuestionOptional` — `name? : T` (argument becomes `T?`)
/// - `Default(src)` — `name~ : T = ` where `src` is raw expression source
#derive(Debug, Eq)
pub(all) enum ParamKind {
  Positional
  DiscardPositional
  Labelled
  QuestionOptional
  Default(String)
}

///|
/// A single parameter on a function, method, trait method, or impl.
#derive(Debug, Eq)
pub(all) struct FnParam {
  name : String
  ty : TypeExpr?
  kind : ParamKind
}

///|
/// Positional parameter `name : T`.
pub fn FnParam::positional(name : String, ty : TypeExpr) -> FnParam {
  { name, ty: Some(ty), kind: Positional, }
}

///|
/// `_ : T` discarded positional parameter.
pub fn FnParam::discard(ty : TypeExpr) -> FnParam {
  { name: "_", ty: Some(ty), kind: DiscardPositional, }
}

///|
/// Required labelled parameter `name~ : T`.
pub fn FnParam::labelled(name : String, ty : TypeExpr) -> FnParam {
  { name, ty: Some(ty), kind: Labelled, }
}

///|
/// Optional labelled parameter `name? : T` (callee receives `T?`).
pub fn FnParam::optional(name : String, ty : TypeExpr) -> FnParam {
  { name, ty: Some(ty), kind: QuestionOptional, }
}

///|
/// Labelled parameter with a default value expression `name~ : T = `.
pub fn FnParam::defaulted(
  name : String,
  ty : TypeExpr,
  default_src : String,
) -> FnParam {
  { name, ty: Some(ty), kind: Default(default_src), }
}

///|
/// A top-level function or a method (`self_type` is `Some`).
///
/// `body` is raw MoonBit statement source; `""` renders an empty body.
#derive(Debug, Eq)
pub(all) struct FnDecl {
  self_type : String?
  name : String
  type_params : Array[TypeParam]
  params : Array[FnParam]
  return_type : TypeExpr?
  is_async : Bool
  error : ErrorSpec
  vis : Visibility
  body : String
}

///|
/// A trait method declaration. Set `has_default: true` to emit `= _`.
#derive(Debug, Eq)
pub(all) struct TraitMethod {
  name : String
  type_params : Array[TypeParam]
  params : Array[FnParam]
  return_type : TypeExpr?
  is_async : Bool
  error : ErrorSpec
  has_default : Bool
  attrs : Array[Attribute]
}

///|
/// A `trait` declaration: `[vis] trait Name : Super1 + Super2 { methods... }`.
#derive(Debug, Eq)
pub(all) struct TraitDecl {
  name : String
  vis : Visibility
  supers : Array[TypeExpr]
  methods : Array[TraitMethod]
}

///|
/// `[vis] impl[type_params]  for  with fn (...) { body }`.
#derive(Debug, Eq)
pub(all) struct ImplDecl {
  vis : Visibility
  type_params : Array[TypeParam]
  method_type_params : Array[TypeParam]
  self_type : TypeExpr
  trait_ : TypeExpr
  method_name : String
  params : Array[FnParam]
  return_type : TypeExpr?
  error : ErrorSpec
  body : String
}

///|
/// `[vis] extend  with ::{methods}` — exposes trait methods
/// for dot-call syntax.
#derive(Debug, Eq)
pub(all) struct ExtendDecl {
  vis : Visibility
  type_name : TypeExpr
  trait_ : TypeExpr
  methods : Array[String]
}

///|
/// A top-level `let` or `const` binding.
#derive(Debug, Eq)
pub(all) struct LetDecl {
  name : String
  ty : TypeExpr?
  /// Raw expression source.
  value : String
  is_const : Bool
  vis : Visibility
}

///|
/// A top-level `test` or `async test` block.
#derive(Debug, Eq)
pub(all) struct TestDecl {
  /// `None` for an anonymous `test { ... }`.
  name : String?
  is_async : Bool
  body : String
}

///|
/// `pub using @pkg {entries}` facade re-export. Entry syntax:
/// `"name"` (value), `"type Name"`, `"trait Name"`.
#derive(Debug, Eq)
pub(all) struct UsingDecl {
  pkg : String
  entries : Array[String]
  vis : Visibility
}

///|
/// Any top-level MoonBit declaration representable in a `SourceFile`.
#derive(Debug, Eq)
pub(all) enum Decl {
  Struct(StructDecl)
  Enum(EnumDecl)
  Suberror(SuberrorDecl)
  Newtype(NewtypeDecl)
  AbstractType(AbstractTypeDecl)
  Alias(AliasDecl)
  Trait(TraitDecl)
  Impl(ImplDecl)
  Extend(ExtendDecl)
  Fn(FnDecl)
  Let(LetDecl)
  Test(TestDecl)
  Using(UsingDecl)
  /// Verbatim MoonBit source (parsed and formatted at render time; may
  /// contain multiple top-level items). The enclosing `Item`'s doc comment
  /// and attributes are IGNORED — put docs and attributes inside `src`.
  Raw(String)
}