// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Konstantin Vyatkin <tino@vtkn.io>

use serde::{Deserialize, Serialize};

/// Identifies the parser backend that produced a [`crate::LanguageAnalysis`].
///
/// Surfaced in JSON output and snapshots so parity work and migration
/// snapshots can tell which backend was active. Per the rewrite plan, parser
/// choice is internal — there is no user-facing override flag — but the
/// label is still useful in reports.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AnalysisBackend {
    TreeSitter,
    /// Ruff parser + semantic. Reserved for the Phase 6 Python migration.
    PythonRuff,
    /// Oxc parser. Reserved for the Phase 7 TypeScript/JS migration.
    Oxc,
    /// Mago syntax. Reserved for the Phase 8 PHP migration.
    Mago,
    /// `ruby-prism`. Reserved for the Phase 9 Ruby migration.
    Prism,
    /// rust-analyzer's `ra_ap_syntax` parser. Used by `mehen-rust` from
    /// Phase 9 of the rewrite onward, replacing tree-sitter-rust.
    RaApSyntax,
    /// Pulldown-cmark parser used by `mehen-markdown`.
    PulldownCmark,
    /// sqruff (`quarylabs/sqruff`) dialect-aware SQL parser. Used by
    /// `mehen-sql`. sqruff parses SQL into a single dialect-agnostic
    /// `SyntaxKind` CST and ships the CTE/scope/wildcard analysis the SQL
    /// metric family consumes (see `design-docs/sql_parser_comparison.md`).
    Sqruff,
    /// A parser generated from an ANTLR v4 grammar, running on the
    /// `antlr4_runtime` Rust runtime (`ophi-dev/antlr-rust-runtime`). Used
    /// by `mehen-kotlin` (official Kotlin spec grammar) and the substrate
    /// for any future ANTLR-backed analyzer. The grammar's `.g4` files are
    /// vendored per analyzer crate and the Rust parser/lexer modules are
    /// generated by `cargo xtask antlr generate <lang>` (cf. the
    /// tree-sitter kind-enum generator).
    Antlr,
    /// Anything not yet covered.
    Other(String),
}

impl AnalysisBackend {
    pub fn label(&self) -> &str {
        match self {
            AnalysisBackend::TreeSitter => "tree-sitter",
            AnalysisBackend::PythonRuff => "python-ruff",
            AnalysisBackend::Oxc => "oxc",
            AnalysisBackend::Mago => "mago",
            AnalysisBackend::Prism => "prism",
            AnalysisBackend::RaApSyntax => "rust-ra-ap-syntax",
            AnalysisBackend::PulldownCmark => "pulldown-cmark",
            AnalysisBackend::Sqruff => "sqruff",
            AnalysisBackend::Antlr => "antlr",
            AnalysisBackend::Other(s) => s.as_str(),
        }
    }
}
