package benchmarks

import (
	"testing"

	"gopkg.in/yaml.v3"
)

// Sample server config YAML for benchmarking
const sampleServerConfig = `
base_path: /api/v1
data_dir: /data/fetchml
auth:
  type: jwt
  secret: "super-secret-key-for-benchmarking-only"
  token_ttl: 3600
server:
  address: :8080
  tls:
    enabled: true
    cert_file: /etc/ssl/certs/server.crt
    key_file: /etc/ssl/private/server.key
security:
  max_request_size: 1048576
  rate_limit: 100
  cors_origins:
    - https://app.fetchml.com
    - https://admin.fetchml.com
queue:
  backend: redis
  sqlite_path: /data/queue.db
  filesystem_path: /data/queue
  fallback_to_filesystem: true
redis:
  addr: localhost:6379
  password: ""
  db: 0
  pool_size: 50
database:
  driver: postgres
  dsn: postgres://user:pass@localhost/fetchml?sslmode=disable
  max_connections: 100
logging:
  level: info
  format: json
  output: stdout
resources:
  max_cpu_per_task: 8
  max_memory_per_task: 32
  max_gpu_per_task: 1
monitoring:
  enabled: true
  prometheus_port: 9090
`

type BenchmarkServerConfig struct {
	BasePath   string                    `yaml:"base_path"`
	DataDir    string                    `yaml:"data_dir"`
	Auth       BenchmarkAuthConfig       `yaml:"auth"`
	Server     BenchmarkServerSection    `yaml:"server"`
	Security   BenchmarkSecurityConfig   `yaml:"security"`
	Queue      BenchmarkQueueConfig      `yaml:"queue"`
	Redis      BenchmarkRedisConfig      `yaml:"redis"`
	Database   BenchmarkDatabaseConfig   `yaml:"database"`
	Logging    BenchmarkLoggingConfig    `yaml:"logging"`
	Resources  BenchmarkResourceConfig   `yaml:"resources"`
	Monitoring BenchmarkMonitoringConfig `yaml:"monitoring"`
}

type BenchmarkAuthConfig struct {
	Type     string `yaml:"type"`
	Secret   string `yaml:"secret"`
	TokenTTL int    `yaml:"token_ttl"`
}

type BenchmarkServerSection struct {
	Address string             `yaml:"address"`
	TLS     BenchmarkTLSConfig `yaml:"tls"`
}

type BenchmarkTLSConfig struct {
	Enabled  bool   `yaml:"enabled"`
	CertFile string `yaml:"cert_file"`
	KeyFile  string `yaml:"key_file"`
}

type BenchmarkSecurityConfig struct {
	MaxRequestSize int      `yaml:"max_request_size"`
	RateLimit      int      `yaml:"rate_limit"`
	CORSOrigins    []string `yaml:"cors_origins"`
}

type BenchmarkQueueConfig struct {
	Backend              string `yaml:"backend"`
	SQLitePath           string `yaml:"sqlite_path"`
	FilesystemPath       string `yaml:"filesystem_path"`
	FallbackToFilesystem bool   `yaml:"fallback_to_filesystem"`
}

type BenchmarkRedisConfig struct {
	Addr     string `yaml:"addr"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
	PoolSize int    `yaml:"pool_size"`
}

type BenchmarkDatabaseConfig struct {
	Driver         string `yaml:"driver"`
	DSN            string `yaml:"dsn"`
	MaxConnections int    `yaml:"max_connections"`
}

type BenchmarkLoggingConfig struct {
	Level  string `yaml:"level"`
	Format string `yaml:"format"`
	Output string `yaml:"output"`
}

type BenchmarkResourceConfig struct {
	MaxCPUPerTask    int `yaml:"max_cpu_per_task"`
	MaxMemoryPerTask int `yaml:"max_memory_per_task"`
	MaxGPUPerTask    int `yaml:"max_gpu_per_task"`
}

type BenchmarkMonitoringConfig struct {
	Enabled        bool `yaml:"enabled"`
	PrometheusPort int  `yaml:"prometheus_port"`
}

// BenchmarkConfigYAMLUnmarshal profiles YAML config parsing
// Tier 3 C++ candidate: Fast binary config format
func BenchmarkConfigYAMLUnmarshal(b *testing.B) {
	data := []byte(sampleServerConfig)

	b.ReportAllocs()

	for b.Loop() {
		var cfg BenchmarkServerConfig
		err := yaml.Unmarshal(data, &cfg)
		if err != nil {
			b.Fatal(err)
		}
	}
}

// BenchmarkConfigYAMLUnmarshalLarge profiles large config parsing
func BenchmarkConfigYAMLUnmarshalLarge(b *testing.B) {
	// Create larger config with more nested data
	largeConfig := sampleServerConfig + `
  extra_section:
    items:
      - id: item1
        name: "First Item"
        value: 100
      - id: item2
        name: "Second Item"
        value: 200
      - id: item3
        name: "Third Item"
        value: 300
`
	data := []byte(largeConfig)

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		var cfg BenchmarkServerConfig
		err := yaml.Unmarshal(data, &cfg)
		if err != nil {
			b.Fatal(err)
		}
	}
}
