-- lx syntax
local syntax = require("core.syntax")

-- Apache .htaccess syntax (Apache 2.x focused)
syntax.add({
	name = "Apache .htaccess",
	shortname = "htaccess",
	author = "liqube",
	filenames = ".htaccess",
	comment = "#",

	patterns = {
		{ pattern = "#.-\n", type = "comment" },
		{ pattern = { '"', '"', "\\" }, type = "string" },
		{ pattern = { "'", "'", "\\" }, type = "string" },

		-- Apache section tags and their closing variants
		{ pattern = "</?IfModule%f[%s>]", type = "meta" },
		{ pattern = "</?FilesMatch%f[%s>]", type = "meta" },
		{ pattern = "</?Files%f[%s>]", type = "meta" },
		{ pattern = "</?If%f[%s>]", type = "meta" },

		-- Rewrite variables and regex character classes
		-- Consistent %{VAR} / %{VAR:...} variable highlighting (underscores, digits, mixed case)

		-- %{HTTP:Authorization}, %{ENV:PATH}, .
		{ pattern = "%%{[%a_][%w_]*:[^}]+}", type = "meta" },

		-- %{REQUEST_URI}, %{REQUEST_FILENAME}, .
		{ pattern = "%%{[%a_][%w_]*}", type = "meta" },

		-- Regex tokens used in RewriteRule/RewriteCond arguments
		
		-- Anchored regex token (non-greedy, stops before ws/flags)
		{ pattern = "%f[%S]%^[^%s%[]+", type = "string" },

		-- RewriteRule flag block: [L,R=301,NC,QSA]
		{ pattern = "%[[A-Za-z][A-Za-z0-9_=,:%-]*%]", type = "meta" },

		-- [A-Z], [^/], [0-9_-], .
		{ pattern = "%[[^%]\n]+%]", type = "string" },

		-- Escaped regex chars: \. \- \/
		{ pattern = "\\.", type = "string" },

		-- Flags: -f -d -L etc
		{ pattern = "%f[%S]%-[%w_%-]+", type = "operator" },

		-- Regex/rewrite chars
		{ pattern = "[%*%+%?%^%$%.%(%){}/|!]", type = "operator" },

		{ pattern = "%d+%.?%d*", type = "number" },		
		{ pattern = "[%a_][%w_]*", type = "symbol" },
	},

	symbols = {
		-- Wrapper Tags
		["<Directory"] = "keyword2", -- Scope to directory
		["</Directory>"] = "keyword2",
		["<Files"] = "keyword2", -- Scope to specific file
		["</Files>"] = "keyword2",
		["<FilesMatch"] = "keyword2", -- Scope by regex
		["</FilesMatch>"] = "keyword2",
		["<IfModule"] = "keyword2", -- Conditional on module existence
		["</IfModule>"] = "keyword2",
		["<Limit"] = "keyword2", -- Restrict by HTTP method
		["</Limit>"] = "keyword2",
		["<If"] = "keyword2", -- Logic expression (Apache 2.4+)
		["</If>"] = "keyword2",

		-- Authentication & Authorization
		["AuthType"] = "keyword", -- Method (Basic, Digest)
		["AuthName"] = "keyword", -- Realm title
		["AuthUserFile"] = "keyword", -- Path to .htpasswd
		["AuthGroupFile"] = "keyword", -- Path to group file
		["Require"] = "keyword", -- Access control (2.4+)
		["RequireUser"] = "keyword", -- Specific user requirement
		["RequireGroup"] = "keyword", -- Specific group requirement
		["RequireValidUser"] = "keyword", -- Any valid login
		["Allow"] = "keyword", -- Legacy access (2.2)
		["Deny"] = "keyword", -- Legacy access (2.2)
		["Order"] = "keyword", -- Legacy evaluation order
		["Satisfy"] = "keyword", -- Logic between auth and access

		-- Mod_Rewrite (URL Manipulation)
		["RewriteEngine"] = "keyword", -- Enable/Disable engine
		["RewriteBase"] = "keyword", -- Base URL for relative paths
		["RewriteRule"] = "keyword", -- Mapping rule
		["RewriteCond"] = "keyword", -- Rule condition
		["RewriteMap"] = "keyword", -- External mapping lookup
		["RewriteOptions"] = "keyword", -- Engine tuning

		-- Redirects & Headers
		["Redirect"] = "keyword", -- Simple URL mapping
		["RedirectMatch"] = "keyword", -- Regex URL mapping
		["RedirectPermanent"] = "keyword", -- 301 Redirect
		["RedirectTemp"] = "keyword", -- 302 Redirect
		["Header"] = "keyword", -- Manipulate HTTP headers
		["RequestHeader"] = "keyword", -- Manipulate incoming headers

		-- Environment & Handlers
		["SetEnv"] = "keyword", -- Set environment variable
		["SetEnvIf"] = "keyword", -- Conditional variable
		["UnsetEnv"] = "keyword", -- Remove variable
		["AddType"] = "keyword", -- Map extension to MIME
		["AddHandler"] = "keyword", -- Map extension to handler
		["AddOutputFilter"] = "keyword", -- Process response via filter

		-- Directory & File Behavior
		["Options"] = "keyword", -- Server features config
		["DirectoryIndex"] = "keyword", -- Default file for directory
		["ErrorDocument"] = "keyword", -- Custom error page path
		["AddDefaultCharset"] = "keyword", -- Default encoding
		["DefaultType"] = "keyword", -- Fallback MIME type
		["FileETag"] = "keyword", -- Cache validator config

		-- Directive Values (keyword2)
		["All"] = "keyword2",
		["None"] = "keyword2",
		["Indexes"] = "keyword2", -- Allow directory listing
		["FollowSymLinks"] = "keyword2", -- Allow symbolic links
		["SymLinksIfOwnerMatch"] = "keyword2", -- Secure symlinks
		["ExecCGI"] = "keyword2", -- Allow CGI execution
		["Includes"] = "keyword2", -- Allow SSI
		["IncludesNOEXEC"] = "keyword2", -- SSI without #exec
		["MultiViews"] = "keyword2", -- Content negotiation
		["any"] = "keyword2", -- Require any
		["granted"] = "keyword2", -- Require granted

		-- Literals
		["on"] = "literal",
		["off"] = "literal",
		["all"] = "literal",
		["none"] = "literal",
		["GET"] = "literal",
		["POST"] = "literal",
		["HEAD"] = "literal",
		["PUT"] = "literal",
		["DELETE"] = "literal",
	},
})
