"""
Programming language definitions.
Maps language names to file extensions and display info.
"""

# (display_name, [extensions], syntax_name_for_prompt)
LANGUAGES = {
    "ABAP":             ([".abap", ".abs"], "ABAP"),
    "Ada":              ([".adb", ".ads", ".ada"], "Ada"),
    "AppleScript":      ([".applescript", ".scpt"], "AppleScript"),
    "Assembly (x86)":   ([".asm", ".s"], "x86 Assembly"),
    "AutoHotkey":       ([".ahk"], "AutoHotkey"),
    "Bash / Shell":     ([".sh", ".bash"], "Bash"),
    "Batch":            ([".bat", ".cmd"], "Windows Batch"),
    "C":                ([".c", ".h"], "C"),
    "C#":               ([".cs"], "C#"),
    "C++":              ([".cpp", ".cxx", ".cc", ".hpp", ".hxx", ".h"], "C++"),
    "Clojure":          ([".clj", ".cljs", ".cljc", ".edn"], "Clojure"),
    "COBOL":            ([".cbl", ".cob", ".cobol"], "COBOL"),
    "CoffeeScript":     ([".coffee"], "CoffeeScript"),
    "Crystal":          ([".cr"], "Crystal"),
    "CSS":              ([".css"], "CSS"),
    "D":                ([".d"], "D"),
    "Dart":             ([".dart"], "Dart"),
    "Delphi / Pascal":  ([".pas", ".dpr", ".dpk"], "Delphi/Pascal"),
    "Elixir":           ([".ex", ".exs"], "Elixir"),
    "Elm":              ([".elm"], "Elm"),
    "Erlang":           ([".erl", ".hrl"], "Erlang"),
    "F#":               ([".fs", ".fsi", ".fsx"], "F#"),
    "Fortran":          ([".f", ".f90", ".f95", ".f03", ".f08", ".for"], "Fortran"),
    "Go":               ([".go"], "Go"),
    "Groovy":           ([".groovy", ".gvy", ".gy"], "Groovy"),
    "Hack":             ([".hack", ".hh"], "Hack"),
    "Haskell":          ([".hs", ".lhs"], "Haskell"),
    "HTML":             ([".html", ".htm"], "HTML"),
    "Java":             ([".java"], "Java"),
    "JavaScript":       ([".js", ".mjs", ".cjs"], "JavaScript"),
    "Julia":            ([".jl"], "Julia"),
    "Kotlin":           ([".kt", ".kts"], "Kotlin"),
    "LabVIEW (G)":      ([".vi"], "LabVIEW"),
    "Lisp (Common)":    ([".lisp", ".lsp", ".cl"], "Common Lisp"),
    "Lua":              ([".lua"], "Lua"),
    "MATLAB":           ([".m", ".mat"], "MATLAB"),
    "Nim":              ([".nim"], "Nim"),
    "Objective-C":      ([".m", ".mm"], "Objective-C"),
    "OCaml":            ([".ml", ".mli"], "OCaml"),
    "Perl":             ([".pl", ".pm"], "Perl"),
    "PHP":              ([".php", ".phtml"], "PHP"),
    "PL/SQL":           ([".pls", ".plb", ".sql"], "PL/SQL"),
    "PowerShell":       ([".ps1", ".psm1", ".psd1"], "PowerShell"),
    "Prolog":           ([".pl", ".pro", ".P"], "Prolog"),
    "Python":           ([".py", ".pyw"], "Python"),
    "R":                ([".r", ".R"], "R"),
    "Racket":           ([".rkt"], "Racket"),
    "Ruby":             ([".rb"], "Ruby"),
    "Rust":             ([".rs"], "Rust"),
    "SAS":              ([".sas"], "SAS"),
    "Scala":            ([".scala", ".sc"], "Scala"),
    "Scheme":           ([".scm", ".ss"], "Scheme"),
    "Scratch":          ([".sb3", ".sb2"], "Scratch"),
    "SCSS / Sass":      ([".scss", ".sass"], "SCSS"),
    "Smalltalk":        ([".st"], "Smalltalk"),
    "Solidity":         ([".sol"], "Solidity"),
    "SQL":              ([".sql"], "SQL"),
    "Swift":            ([".swift"], "Swift"),
    "Tcl":              ([".tcl"], "Tcl"),
    "T-SQL":            ([".sql"], "T-SQL"),
    "TypeScript":       ([".ts", ".tsx"], "TypeScript"),
    "VBA":              ([".bas", ".cls", ".frm"], "VBA"),
    "VB.NET":           ([".vb"], "VB.NET"),
    "Verilog":          ([".v", ".sv"], "Verilog"),
    "VHDL":             ([".vhd", ".vhdl"], "VHDL"),
    "Zig":              ([".zig"], "Zig"),

    # --- レガシー / 基幹系 / メインフレーム（移行需要が高い） ---
    "RPG (IBM)":          ([".rpg", ".rpgle", ".sqlrpgle"], "IBM RPG"),
    "REXX":               ([".rexx", ".rex"], "REXX"),
    "PL/I":               ([".pli", ".pl1"], "PL/I"),
    "JCL":                ([".jcl"], "IBM JCL"),
    "MUMPS (M)":          ([".mumps"], "MUMPS / M"),
    "Natural":            ([".nsp", ".nsn"], "Software AG Natural"),
    "Progress 4GL / ABL": ([".p", ".w"], "OpenEdge ABL"),
    "PowerBuilder":       ([".pbl", ".srw", ".sru"], "PowerBuilder PowerScript"),
    "Visual FoxPro":      ([".prg", ".fxp"], "Visual FoxPro"),
    "ColdFusion (CFML)":  ([".cfm", ".cfc"], "CFML"),
    "VBScript":           ([".vbs"], "VBScript"),
    "Visual Basic 6":     ([".vbp", ".vb6"], "Visual Basic 6"),
    "Informix 4GL":       ([".4gl"], "Informix 4GL"),
    "Forth":              ([".fth", ".forth", ".4th"], "Forth"),
    "Eiffel":             ([".e"], "Eiffel"),
    "Modula-2":           ([".mod", ".m2"], "Modula-2"),
    "Oberon":             ([".obn"], "Oberon"),
    "ALGOL":              ([".alg"], "ALGOL"),
    "Simula":             ([".sim"], "Simula"),
    "PostScript":         ([".ps", ".eps"], "PostScript"),

    # --- モダン / 新興 ---
    "GDScript":           ([".gd"], "GDScript (Godot)"),
    "Haxe":               ([".hx", ".hxml"], "Haxe"),
    "Raku":               ([".raku", ".rakumod", ".p6"], "Raku"),
    "ReScript":           ([".res", ".resi"], "ReScript"),
    "Reason":             ([".re", ".rei"], "ReasonML"),
    "PureScript":         ([".purs"], "PureScript"),
    "Standard ML":        ([".sml"], "Standard ML"),
    "Vala":               ([".vala", ".vapi"], "Vala"),
    "Pike":               ([".pike"], "Pike"),
    "Pony":               ([".pony"], "Pony"),
    "Odin":               ([".odin"], "Odin"),
    "V":                  ([".vsh"], "V (vlang)"),
    "Mojo":               ([".mojo"], "Mojo"),
    "Carbon":             ([".carbon"], "Carbon"),
    "Gleam":              ([".gleam"], "Gleam"),
    "Ballerina":          ([".bal"], "Ballerina"),
    "Q#":                 ([".qs"], "Q# (Quantum)"),
    "Cython":             ([".pyx", ".pxd"], "Cython"),
    "WebAssembly (WAT)":  ([".wat", ".wast"], "WebAssembly Text"),

    # --- クエリ / IaC / ドメイン固有 ---
    "GraphQL":            ([".graphql", ".gql"], "GraphQL"),
    "Terraform (HCL)":    ([".tf", ".tfvars"], "Terraform HCL"),
    "Apex (Salesforce)":  ([".cls", ".trigger"], "Salesforce Apex"),

    # --- 科学技術計算 / 統計 / 配列指向 ---
    "Wolfram":            ([".wl", ".wls", ".nb"], "Wolfram Language"),
    "APL":                ([".apl"], "APL"),
    "Stata":              ([".do", ".ado"], "Stata"),
    "SPSS":               ([".sps"], "SPSS Syntax"),
    "Octave":             ([".octave"], "GNU Octave"),
}

# Sorted list for UI
LANGUAGE_NAMES = sorted(LANGUAGES.keys())


def get_extensions(lang_name: str) -> list:
    """Return file extensions for a language."""
    if lang_name in LANGUAGES:
        return LANGUAGES[lang_name][0]
    return []


def get_syntax_name(lang_name: str) -> str:
    """Return the syntax name used in prompts."""
    if lang_name in LANGUAGES:
        return LANGUAGES[lang_name][1]
    return lang_name


def detect_language_by_ext(filepath: str) -> str | None:
    """Try to detect language from file extension."""
    ext = "." + filepath.rsplit(".", 1)[-1] if "." in filepath else ""
    ext_lower = ext.lower()
    for name, (exts, _) in LANGUAGES.items():
        if ext_lower in [e.lower() for e in exts]:
            return name
    return None
