///|
/// moonlint — a config-driven convention linter for source trees.
///
/// This is a MoonBit port of `wot-lynx-ui`'s `scripts/review-lint.mjs`.
/// It scans a directory tree, applies a set of declarative rules to each
/// source file, and reports violations. Rules are loaded from a JSON config
/// so the same engine can enforce any project's conventions.
///
/// Design notes:
/// - Regex-free core matchers (`Contains` / `HasPrefix` / `HasSuffix`) cover
///   the common cases cheaply; a `Regex` variant is available for the rest.
/// - Comments are blanked out (preserving line numbers and column offsets)
///   before matching, so rule authors never match inside comments.
/// - A hash-based baseline lets an existing codebase adopt the linter
///   incrementally: known findings are exempted until they are fixed.

///|
/// Severity of a rule violation.
#derive(Debug, Eq)
pub enum Severity {
  Error
  Warning
}

///|
/// Convenience constructors (pub so external packages can build values).
pub fn Severity::error() -> Severity {
  Error
}

///|
/// Construct a non-blocking warning severity for external rule definitions.
pub fn Severity::warning() -> Severity {
  Warning
}

///|
/// Render a severity as a lowercase tag used in reports.
pub fn Severity::tag(self : Severity) -> String {
  match self {
    Error => "error"
    Warning => "warning"
  }
}

///|
/// Parse a severity from its lowercase tag. Unknown values default to
/// `Warning` so a typo in config never silently drops an error-level rule.
pub fn Severity::parse(self : String) -> Severity {
  match self {
    "error" => Error
    _ => Warning
  }
}

///|
/// A single line-matching primitive, or a boolean combination of matchers.
/// A rule pairs one `match` kind with an optional `unless` kind (a negative
/// guard) to express "match X but not Y"; the combinators below express the
/// rest — `all` / `any` / `not` nest arbitrarily deep.
#derive(Debug, Eq)
pub enum MatchKind {
  /// Line contains the substring.
  Contains(String)
  /// Line starts with the substring.
  HasPrefix(String)
  /// Line ends with the substring.
  HasSuffix(String)
  /// Line matches the regular expression.
  Regex(String)
  /// Trimmed line equals the string exactly.
  Equals(String)
  /// Line contains the string as a whole word (bounded by non-word chars).
  Word(String)
  /// Every sub-matcher must match; an empty list matches every line.
  All(Array[MatchKind])
  /// At least one sub-matcher must match; an empty list matches no line.
  Any(Array[MatchKind])
  /// The sub-matcher must not match.
  Not(MatchKind)
}

///|
/// A declarative lint rule.
#derive(Debug)
pub struct Rule {
  /// Stable identifier, e.g. `vue/import-from-vue`.
  id : String
  severity : Severity
  /// Human-readable explanation shown when the rule fires.
  message : String
  /// File extensions this rule applies to, e.g. `[".vue", ".ts"]`.
  /// An empty list means "all scanned files".
  extensions : Array[String]
  /// The primary matcher.
  match_ : MatchKind
  /// Optional negative guard: if this also matches, the finding is skipped.
  unless : MatchKind?
  /// Paths this rule applies to. An empty scope means "every file".
  scope : Scope
  /// Whether this rule also matches inside comments. Line rules normally see
  /// the comment-stripped view of a file (see `comment.mbt`), which keeps a
  /// stringified script or a URL fragment from looking like code; a rule that
  /// targets comments themselves — a no-TODO rule, say — opts into the raw
  /// view instead. `false` keeps the historical stripped view.
  match_comments : Bool
  /// Whether this rule matches literal text. The default code view blanks the
  /// contents of string, template and regex literals (JavaScript/TypeScript
  /// family), so `const q = "FORBIDDEN"` no longer looks like code; a rule
  /// whose target only exists as a literal value — an attribute value such as
  /// `target="_blank"`, a module specifier such as `from 'react'` — opts in
  /// with `true` and matches the literal view instead. Ignored when
  /// `match_comments` is set, because the raw view already contains literals.
  match_literals : Bool
}

///|
/// A single rule violation located in a file.
#derive(Debug)
pub struct Finding {
  rule_id : String
  severity : Severity
  message : String
  /// Path relative to the scan root.
  file : String
  /// 1-based line number.
  line : Int32
  /// 1-based column of the first match on that line.
  column : Int32
  /// The offending source line (trimmed).
  snippet : String
}

///|
/// A scan failure. These are operational errors, not lint findings.
#derive(Debug)
pub struct Diagnostic {
  file : String
  message : String
}

///|
/// A file that entered the scan but was not read.
#derive(Debug)
pub struct SkippedFile {
  /// Path relative to the scan root.
  file : String
  /// Why the file was not read, e.g. `no-applicable-rule`.
  reason : String
}

///|
/// Scan metadata attached to machine-readable reports.
#derive(Debug)
pub struct ScanInfo {
  /// Scan mode tag, e.g. `tree`, `staged`, `glob`.
  mode : String
  /// Absolute scan root.
  root : String
  /// Effective extension filter; an empty list means "all files".
  extensions : Array[String]
}

///|
/// Build the scan metadata carried by machine-readable reports.
pub fn ScanInfo::new(
  mode : String,
  root : String,
  extensions : Array[String],
) -> ScanInfo {
  { mode, root, extensions, }
}

///|
/// A parsed configuration: the rule set plus the global file scope.
#derive(Debug)
pub struct LintConfig {
  /// The configured rules, in declaration order.
  rules : Array[Rule]
  /// File-level rules evaluated once per file.
  file_rules : Array[FileRule]
  /// Project-level structural rules evaluated across all files at once.
  structural_rules : Array[StructuralRule]
  /// The global scope applied to every scan.
  scope : Scope
  /// Path-alias prefixes for cross-file import resolution, as ordered
  /// `(prefix, target)` pairs. A bare specifier `prefix/rest` resolves to the
  /// root-relative path `target/rest` before extension probing, so aliases
  /// such as `@/` → `src` let `no_unused_exports` follow them. Empty by
  /// default; relative specifiers are unaffected.
  aliases : Array[(String, String)]
}

///|
/// Naming convention for file-level naming checks.
#derive(Debug, Eq)
pub enum NamingStyle {
  CamelCase
  PascalCase
  SnakeCase
  KebabCase
}

///|
/// Parse a naming style from its string tag.
pub fn NamingStyle::parse(s : String) -> NamingStyle? {
  match s {
    "camelCase" => Some(CamelCase)
    "PascalCase" => Some(PascalCase)
    "snake_case" => Some(SnakeCase)
    "kebab-case" => Some(KebabCase)
    _ => None
  }
}

///|
/// Line ending style for file-level checks.
#derive(Debug, Eq)
pub enum LineEndingStyle {
  /// Unix line endings (LF only).
  Lf
  /// Windows line endings (CRLF only).
  Crlf
}

///|
/// Parse a line ending style from its string tag.
pub fn LineEndingStyle::parse(s : String) -> LineEndingStyle? {
  match s {
    "lf" => Some(Lf)
    "crlf" => Some(Crlf)
    _ => None
  }
}

///|
/// A file-level check evaluated once per file, not per line.
#derive(Debug, Eq)
pub enum FileCheck {
  /// File must not exceed N lines.
  MaxLines(Int32)
  /// No line may exceed N characters.
  MaxLineLength(Int32)
  /// No trailing whitespace on any line.
  NoTrailingWhitespace
  /// No tab characters anywhere in the file.
  NoTabs
  /// File must end with a newline.
  FinalNewline
  /// No duplicate lines in the file.
  /// No duplicate lines in the file. The payload is the minimum trimmed line
  /// length a line must have to count: repeated structural lines such as `}`,
  /// ``, `/**` or `return` would otherwise flood a normal project.
  NoDuplicateLines(Int32)
  /// File name (without extension) must follow a naming convention.
  FileNaming(NamingStyle)
  /// File must not exceed N bytes.
  MaxFileSize(Int32)
  /// File must not be empty.
  NoEmptyFiles
  /// File must use consistent line endings.
  LineEnding(LineEndingStyle)
  /// File must not start with a UTF-8 BOM.
  NoBom
}

///|
/// A file-level lint rule.
#derive(Debug)
pub struct FileRule {
  /// Stable identifier, e.g. `file/max-lines`.
  id : String
  severity : Severity
  /// Human-readable explanation shown when the rule fires.
  message : String
  /// File extensions this rule applies to. Empty means "all scanned files".
  extensions : Array[String]
  /// The file-level check to perform.
  check : FileCheck
  /// Paths this rule applies to. An empty scope means "every file".
  scope : Scope
}

///|
/// A cross-file structural check evaluated over the whole parsed project
/// index. Unlike line and file checks these require a second pass that sees
/// every file at once, so they are expressed as project-level checks.
#derive(Debug, Eq)
pub enum StructuralCheck {
  /// An imported binding is never referenced in the importing file.
  NoUnusedImports
  /// The same module is imported by more than one import statement.
  NoDuplicateImports
  /// A relative import cannot be resolved to any scanned file.
  NoUnresolvedRelativeImports
  /// The file takes part in a cycle of relative imports.
  NoImportCycles
  /// `export default` is used.
  NoDefaultExport
  /// A relative import climbs more than N directories with `../`.
  NoDeepRelativeImports(Int32)
  /// `require()` is used in an ES module codebase.
  NoRequireImports
  /// An exported name is never imported by any other scanned file.
  NoUnusedExports
  /// A public top-level MoonBit item has no `///|` doc comment.
  MoonbitRequirePublicDoc
  /// A MoonBit declaration name does not follow the language convention.
  MoonbitNaming
  /// A syntactic method call named unwrap, excluding literal text and tests.
  MoonbitNoUnwrap
  /// A syntactic unqualified panic call, excluding declarations and tests.
  MoonbitNoPanic
  /// A syntactic unqualified println call, excluding declarations and tests.
  MoonbitNoPrintln
  /// A TODO/FIXME marker at the start of an actual comment.
  MoonbitNoTodo
}

///|
/// A project-level lint rule.
#derive(Debug)
pub struct StructuralRule {
  /// Stable identifier, e.g. `imports/no-unresolved-relative-imports`.
  id : String
  severity : Severity
  /// Human-readable explanation shown when the rule fires.
  message : String
  /// File extensions this rule applies to. Empty means "all scanned files".
  extensions : Array[String]
  /// The structural check to perform.
  check : StructuralCheck
  /// Paths this rule applies to. An empty scope means "every file".
  scope : Scope
}

///|
/// Aggregate result. Selection is counted after path normalization, extension
/// filtering and de-duplication; each selected file is scanned, skipped or failed.
#derive(Debug)
pub struct LintResult {
  findings : Array[Finding]
  /// Files that passed path, extension and scope filters.
  files_selected : Int32
  /// Files read successfully and linted.
  files_scanned : Int32
  /// Selected files that were not read, with a reason for each.
  files_skipped : Int32
  /// Files that could not be read, with a diagnostic for each.
  files_failed : Int32
  /// Candidate files removed by the global scope before scanning.
  files_out_of_scope : Int32
  /// Additional dependency files read only to supply structural context.
  context_files_scanned : Int32
  /// Context read failures make cross-file checks inconclusive.
  context_files_failed : Int32
  /// Captured Git commit id or index label; None means working-tree input.
  source_snapshot : String?
  /// Findings suppressed by the baseline.
  baseline_hits : Int32
  diagnostics : Array[Diagnostic]
  skipped_files : Array[SkippedFile]
}

///|
/// Backwards-compatible constructor for callers supplying successful findings.
pub fn LintResult::new(
  findings : Array[Finding],
  files_scanned : Int32,
  baseline_hits : Int32,
) -> LintResult {
  {
    findings,
    files_selected: files_scanned,
    files_scanned,
    files_skipped: 0,
    files_failed: 0,
    files_out_of_scope: 0,
    context_files_scanned: 0,
    context_files_failed: 0,
    source_snapshot: None,
    baseline_hits,
    diagnostics: [],
    skipped_files: [],
  }
}

///|
/// Replace displayed findings without losing the run's IO counters.
pub fn LintResult::with_findings(
  self : LintResult,
  findings : Array[Finding],
  baseline_hits : Int32,
) -> LintResult {
  {
    findings,
    files_selected: self.files_selected,
    files_scanned: self.files_scanned,
    files_skipped: self.files_skipped,
    files_failed: self.files_failed,
    files_out_of_scope: self.files_out_of_scope,
    context_files_scanned: self.context_files_scanned,
    context_files_failed: self.context_files_failed,
    source_snapshot: self.source_snapshot,
    baseline_hits,
    diagnostics: self.diagnostics,
    skipped_files: self.skipped_files,
  }
}

///|
/// Whether the result contains any error-severity finding.
pub fn LintResult::has_errors(self : LintResult) -> Bool {
  self.findings.any(f => f.severity == Severity::Error)
}

///|
/// Count findings by severity.
pub fn LintResult::count_by(self : LintResult, sev : Severity) -> Int32 {
  self.findings.count_if(f => f.severity == sev)
}

///|
/// Expose the derived impls as regular methods so callers outside this
/// package can use them without relying on implicit impl promotion.

///|
pub extend Severity with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend Severity with Eq::{equal, not_equal}

///|
pub extend MatchKind with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend MatchKind with Eq::{equal, not_equal}

///|
pub extend Rule with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend Finding with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend Diagnostic with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend SkippedFile with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend ScanInfo with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend LintConfig with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend NamingStyle with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend NamingStyle with Eq::{equal, not_equal}

///|
pub extend LineEndingStyle with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend LineEndingStyle with Eq::{equal, not_equal}

///|
pub extend FileCheck with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend FileCheck with Eq::{equal, not_equal}

///|
pub extend FileRule with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend StructuralCheck with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend StructuralCheck with Eq::{equal, not_equal}

///|
pub extend StructuralRule with @moonbitlang/core/debug.Debug::{to_repr}

///|
pub extend LintResult with @moonbitlang/core/debug.Debug::{to_repr}