package mellaris

import (
	"git.difuse.io/Difuse/Mellaris/analyzer"
	"git.difuse.io/Difuse/Mellaris/engine"
	gfwio "git.difuse.io/Difuse/Mellaris/io"
	"git.difuse.io/Difuse/Mellaris/modifier"
	"git.difuse.io/Difuse/Mellaris/ruleset"
)

// Config defines IO, worker, and ruleset settings for the engine.
type Config struct {
	IO      IOConfig      `mapstructure:"io" yaml:"io"`
	Workers WorkersConfig `mapstructure:"workers" yaml:"workers"`
	Ruleset RulesetConfig `mapstructure:"ruleset" yaml:"ruleset"`
}

// IOConfig configures packet IO.
type IOConfig struct {
	QueueSize    uint32 `mapstructure:"queueSize" yaml:"queueSize"`
	ReadBuffer   int    `mapstructure:"rcvBuf" yaml:"rcvBuf"`
	WriteBuffer  int    `mapstructure:"sndBuf" yaml:"sndBuf"`
	Local        bool   `mapstructure:"local" yaml:"local"`
	RST          bool   `mapstructure:"rst" yaml:"rst"`
	NumQueues    int    `mapstructure:"numQueues" yaml:"numQueues"`
	MaxPacketLen uint32 `mapstructure:"maxPacketLen" yaml:"maxPacketLen"`

	// PacketIO overrides NFQueue creation when set.
	// When provided, App.Close will call PacketIO.Close.
	PacketIO gfwio.PacketIO `mapstructure:"-" yaml:"-"`
}

// WorkersConfig configures engine worker behavior.
type WorkersConfig struct {
	Count                      int                          `mapstructure:"count" yaml:"count"`
	QueueSize                  int                          `mapstructure:"queueSize" yaml:"queueSize"`
	TCPMaxBufferedPagesTotal   int                          `mapstructure:"tcpMaxBufferedPagesTotal" yaml:"tcpMaxBufferedPagesTotal"`
	TCPMaxBufferedPagesPerConn int                          `mapstructure:"tcpMaxBufferedPagesPerConn" yaml:"tcpMaxBufferedPagesPerConn"`
	UDPMaxStreams              int                          `mapstructure:"udpMaxStreams" yaml:"udpMaxStreams"`
	OverflowPolicy             engine.OverflowPolicy        `mapstructure:"overflowPolicy" yaml:"overflowPolicy"`
	AnalyzerSelectionMode      engine.AnalyzerSelectionMode `mapstructure:"analyzerSelectionMode" yaml:"analyzerSelectionMode"`
}

// RulesetConfig configures built-in rule helpers.
type RulesetConfig struct {
	GeoIp   string `mapstructure:"geoip" yaml:"geoip"`
	GeoSite string `mapstructure:"geosite" yaml:"geosite"`
}

// Options configures rules, analyzers, modifiers, and logging.
type Options struct {
	// RulesFile is a YAML rules file on disk. Mutually exclusive with Rules.
	RulesFile string
	// Rules provides inline expression rules. Mutually exclusive with RulesFile.
	Rules []ruleset.ExprRule

	// Analyzers and Modifiers default to built-ins when nil.
	Analyzers []analyzer.Analyzer
	Modifiers []modifier.Modifier

	// EngineLogger and RulesetLogger default to no-op loggers when nil.
	EngineLogger  engine.Logger
	RulesetLogger ruleset.Logger
}
