import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

/// Macro applied to a module class. It plays three roles, each implemented in its own
/// extension below:
///
/// - `MemberMacro`: binds `@JS`-marked members directly into the module's JS object via the
///   synthesized `_decorateModule`, and emits the resolved module name as a non-optional
///   `_jsName` static (no `Name(…)` DSL element). The `classes:` and `views:` arguments become
///   `_classTypes` and `_viewTypes` statics that `expo-modules-core` registers by type.
///   When the class doesn't already inherit `Module`/`BaseModule`, it also
///   synthesizes the `appContext` storage and `init(appContext:)` those base classes provide.
/// - `MemberAttributeMacro`: stamps `@JavaScriptActor` on `@JS` sync members and
///   `@ModuleDefinitionBuilder` on a `definition()` method.
/// - `ExtensionMacro`: adds the `AnyModule` conformance when the class doesn't inherit it, and an
///   `@unchecked Sendable` conformance when the class isn't `Sendable` yet.
///
/// Inheriting from `Module` is therefore optional — a class can carry any superclass (or
/// none) and still be a module, because everything inheritance used to provide is synthesized.
///
///   @ExpoModule
///   public final class MyModule {
///     public func definition() -> ModuleDefinition {
///       Name("MyModule")
///     }
///
///     @JS
///     func greet(name: String) -> String { "Hi, \(name)" }
///   }
public struct ExpoModuleMacro: MemberMacro {
  public static func expansion(
    of node: AttributeSyntax,
    providingMembersOf declaration: some DeclGroupSyntax,
    conformingTo protocols: [TypeSyntax],
    in context: some MacroExpansionContext
  ) throws -> [DeclSyntax] {
    guard let classDecl = declaration.as(ClassDeclSyntax.self) else {
      throw MacroExpansionErrorMessage("@ExpoModule can only be applied to a class")
    }
    diagnoseNonPublicClass(
      classDecl,
      macroName: "@ExpoModule",
      reason: "register it",
      idPrefix: "expomodule",
      in: context
    )

    // The module name is fully resolved here (the explicit `@ExpoModule("…")` argument, else the
    // class name) and emitted as a non-optional `_jsName` static below — so the
    // macro no longer emits a `Name(…)` DSL entry. Core reads the static for the module name,
    // ahead of its type-name fallback (which then only applies to non-macro DSL modules).
    let moduleName = jsNameArgument(of: node) ?? withoutBackticks(classDecl.name.text)

    // `@JS func`s (sync and async) and `@JS var`s are bound directly into the JS object by the
    // synthesized `_decorateModule` rather than described with a `Function(...)` / `Property(...)`
    // DSL entry.
    var functions: [JSFunction] = []
    var properties: [JSProperty] = []
    var assertions: [MemberConformanceAssertion] = []
    let aliases = TupleTypeAliases(in: classDecl.memberBlock)

    for member in classDecl.memberBlock.members {
      // The types of a `@JS` member are read with the class's tuple aliases resolved.
      let decl = aliases.resolving(member.decl)
      // A generic member is reported by `@JS` and left out, so the build has no errors in its binding.
      if memberHasJSAttribute(decl) && jsGenericParameterClause(of: decl) != nil {
        continue
      }
      // So is a `static` or `class` member: the module object binds only instance members.
      if memberHasJSAttribute(decl),
        let modifiers = decl.asProtocol(WithModifiersSyntax.self)?.modifiers, isTypeLevel(modifiers)
      {
        continue
      }
      if memberHasJSAttribute(decl), let assertion = jsConformanceAssertionBody(for: decl) {
        assertions.append(assertion)
      }

      if let funcDecl = decl.as(FunctionDeclSyntax.self),
        let attribute = funcDecl.attributes.firstAttribute(named: "JS")
      {
        let function = JSFunction(funcDecl: funcDecl, attribute: attribute)
        functions.append(function)
        if let variant = function.asyncVariant {
          functions.append(variant)
        }
        continue
      }

      if let varDecl = decl.as(VariableDeclSyntax.self),
        let attribute = varDecl.attributes.firstAttribute(named: "JS")
      {
        properties.append(contentsOf: collectProperties(varDecl: varDecl, attribute: attribute))
      }
    }

    diagnoseJSNameCollisions(functions: functions, properties: properties, in: context)
    diagnoseReservedJSNames(functions: functions, properties: properties, on: .module, in: context)
    diagnoseEventNameCollisions(in: classDecl.memberBlock, in: context)

    var emitted: [DeclSyntax] = []

    // The fully-resolved module name as a non-optional stored constant. Core reads this
    // instead of the retired `Name(…)` DSL element; it feeds both native registration and the
    // JS object name, so they can't diverge.
    emitted.append("public static let _jsName = \"\(raw: moduleName)\"")

    // Shared object classes are registered by type: core builds each JavaScript class from the
    // `@SharedObject` hooks, with the name from the class's `_jsName`. The element type makes the
    // compiler reject a class that isn't a `SharedObject` subclass. Emitted only when the argument is
    // present, so a module without classes gains nothing.
    let classTypes = classListArgument(of: node, label: "classes")
    if !classTypes.isEmpty {
      let entries = classTypes.map { "\($0).self" }.joined(separator: ", ")
      emitted.append("public static let _classTypes: [SharedObject.Type] = [\(raw: entries)]")
    }

    // View types are registered by type, not spliced as definitions: an `@ExpoView` emits no
    // definition function, so there is nothing to call on each entry. Core reads a view's props
    // type from its `Props` typealias and its event names from that type's `_eventNames`, both
    // static. The element type makes the compiler reject a type that isn't an `@ExpoView`, whose
    // macro adds a conformance to a protocol that refines `AnyNativeView`. Each entry is coerced on
    // its own, because for an array literal of existential metatypes the compiler fails to produce a
    // diagnostic. Emitted only when the argument is present, so a module without views gains nothing.
    let viewTypes = classListArgument(of: node, label: "views")
    if !viewTypes.isEmpty {
      let entries = viewTypes.map { "\($0).self as any AnyNativeView.Type" }.joined(separator: ", ")
      emitted.append("public static let _viewTypes: [any AnyNativeView.Type] = [\(raw: entries)]")
    }

    // `Module`/`BaseModule` already provide `appContext` storage and the
    // `init(appContext:)` requirement, so we only synthesize them for classes that
    // inherit from neither. Each is skipped individually if the user wrote their own,
    // to avoid emitting a duplicate declaration.
    if !inheritsFromAny(classDecl, names: ["Module", "BaseModule"]) {
      if !hasStoredProperty(classDecl, named: "appContext") {
        emitted.append("public weak var appContext: AppContext?")
      }
      if !hasAppContextInitializer(classDecl) {
        emitted.append(
          """
          public required init(appContext: AppContext) {
            self.appContext = appContext
          }
          """
        )
      }
    }

    // Direct JSI binding: one `_decorateModule` that binds each `@JS func` (inlined `setProperty`
    // closure) and each `@JS var` (a `defineProperty` get/set accessor) into the module's JS object.
    // Only emitted when there's at least one member to bind.
    if !functions.isEmpty || !properties.isEmpty {
      emitted.append(buildDecorateJavaScriptObject(functions: functions, properties: properties))
    }

    // The conformance assertions of the `@JS` members, which `@JS` leaves to this macro (see
    // `jsConformanceAssertionBody`).
    if let assertion = classConformanceAssertions(assertions) {
      emitted.append(assertion)
    }

    return emitted
  }
}

extension ExpoModuleMacro: MemberAttributeMacro {
  public static func expansion(
    of node: AttributeSyntax,
    attachedTo declaration: some DeclGroupSyntax,
    providingAttributesFor member: some DeclSyntaxProtocol,
    in context: some MacroExpansionContext
  ) throws -> [AttributeSyntax] {
    // This macro is invoked once per member and may attach more than one attribute,
    // so we collect into an array rather than returning early. The two checks below are
    // independent — a given member matches at most one in practice, but they're written
    // so neither precludes the other.
    var attributes: [AttributeSyntax] = []

    // `@JS` sync members run on the JS thread; stamp `@JavaScriptActor` so isolation is
    // checked at compile time. Skipped when the member already chose an isolation
    // (`nonisolated` or another global actor), or when `.concurrent` or `.withAsync("name")` runs
    // it on the concurrent pool. See `shouldStampJavaScriptActor`.
    if memberHasJSAttribute(member),
      shouldStampJavaScriptActor(on: member, enclosedBy: declaration)
    {
      attributes.append("@JavaScriptActor")
    }

    // `@JS(.concurrent)` is the inverse: instead of the JS-thread stamp the member gets
    // `@concurrent`, so its body runs on the concurrent pool. `shouldStampJavaScriptActor` already
    // skipped the stamp above, leaving the two mutually exclusive.
    if isConcurrentJSMember(member) {
      attributes.append("@concurrent")
    }

    // Apply the result builder to `definition()` so the user doesn't have to. Skipped if
    // they already wrote `@ModuleDefinitionBuilder` themselves, which would otherwise be
    // a duplicate attribute.
    if isModuleDefinitionFunction(member),
      !memberAttributes(of: member).contains(where: { hasAttributeName($0, "ModuleDefinitionBuilder") })
    {
      attributes.append("@ModuleDefinitionBuilder")
    }

    return attributes
  }
}

private func isModuleDefinitionFunction(_ member: some DeclSyntaxProtocol) -> Bool {
  guard let funcDecl = member.as(FunctionDeclSyntax.self),
    funcDecl.name.text == "definition",
    funcDecl.signature.parameterClause.parameters.isEmpty,
    let returnType = funcDecl.signature.returnClause?.type
  else {
    return false
  }
  return baseIdentifier(of: returnType) == "ModuleDefinition"
}

private func hasAttributeName(_ element: AttributeListSyntax.Element, _ name: String) -> Bool {
  guard let attribute = element.as(AttributeSyntax.self) else {
    return false
  }
  return attribute.attributeName.trimmedDescription == name
}

extension ExpoModuleMacro: ExtensionMacro {
  public static func expansion(
    of node: AttributeSyntax,
    attachedTo declaration: some DeclGroupSyntax,
    providingExtensionsOf type: some TypeSyntaxProtocol,
    conformingTo protocols: [TypeSyntax],
    in context: some MacroExpansionContext
  ) throws -> [ExtensionDeclSyntax] {
    // `protocols` is empty when the compiler already sees the conformance (e.g. it's
    // spelled in the declaration), in which case there's nothing for us to add.
    guard !protocols.isEmpty else {
      return []
    }
    var extensions: [ExtensionDeclSyntax] = []
    // These base types already supply `AnyModule`; emitting a second conformance here
    // would be redundant and error.
    if protocols.contains(where: { $0.trimmedDescription == "AnyModule" }),
      let classDecl = declaration.as(ClassDeclSyntax.self),
      !inheritsFromAny(classDecl, names: ["Module", "BaseModule", "AnyModule"])
    {
      let conformance: DeclSyntax = "extension \(type.trimmed): AnyModule {}"
      extensions.append(conformance.cast(ExtensionDeclSyntax.self))
    }
    if let sendable = uncheckedSendableExtension(for: type, of: declaration, conformingTo: protocols) {
      extensions.append(sendable)
    }
    return extensions
  }
}

// MARK: - Synthesized-member detection

private func hasStoredProperty(_ classDecl: ClassDeclSyntax, named name: String) -> Bool {
  for member in classDecl.memberBlock.members {
    guard let varDecl = member.decl.as(VariableDeclSyntax.self) else {
      continue
    }
    for binding in varDecl.bindings {
      if let identifier = binding.pattern.as(IdentifierPatternSyntax.self),
        identifier.identifier.text == name
      {
        return true
      }
    }
  }
  return false
}

/// True if the class declares an initializer that satisfies the `init(appContext:)` protocol
/// requirement: a single parameter labeled `appContext` whose type is written as `AppContext`
/// (or `…​.AppContext`). Both must match — the label is part of the requirement, so
/// `init(c: AppContext)` does *not* satisfy it (we still synthesize ours), and the type guards
/// against a same-labeled init of an unrelated type. A macro can't resolve types, so this is a
/// syntactic match on the spelled name.
private func hasAppContextInitializer(_ classDecl: ClassDeclSyntax) -> Bool {
  for member in classDecl.memberBlock.members {
    guard let initDecl = member.decl.as(InitializerDeclSyntax.self) else {
      continue
    }
    let params = initDecl.signature.parameterClause.parameters
    guard params.count == 1, let param = params.first else {
      continue
    }
    if param.firstName.text == "appContext", baseIdentifier(of: param.type) == "AppContext" {
      return true
    }
  }
  return false
}
