package main

import (
	"sync"
	"testing"
)

func resetConfigForTest() {
	conf = nil
	confOnce = sync.Once{}
}

func TestInitConfigTLSSettings(t *testing.T) {
	tests := []struct {
		name          string
		args          []string
		wantCert      string
		wantKey       string
		wantLogHealth bool
		wantError     bool
	}{
		{
			name:          "default tls disabled and health-check logs filtered",
			args:          []string{},
			wantCert:      "",
			wantKey:       "",
			wantLogHealth: false,
			wantError:     false,
		},
		{
			name: "override tls files by flags",
			args: []string{
				"--tls-cert-file", "/tmp/server.crt",
				"--tls-key-file", "/tmp/server.key",
			},
			wantCert:      "/tmp/server.crt",
			wantKey:       "/tmp/server.key",
			wantLogHealth: false,
			wantError:     false,
		},
		{
			name: "enable health-check logs by flag",
			args: []string{
				"--log-health-check",
			},
			wantCert:      "",
			wantKey:       "",
			wantLogHealth: true,
			wantError:     false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			resetConfigForTest()
			t.Cleanup(resetConfigForTest)

			err := InitConfig(tt.args)
			if (err != nil) != tt.wantError {
				t.Fatalf("InitConfig() error = %v, wantError %v", err, tt.wantError)
			}

			cfg := GetConfig()
			if cfg == nil {
				t.Fatal("GetConfig() returned nil config")
			}

			if cfg.TLSCertFile != tt.wantCert {
				t.Fatalf("TLSCertFile = %q, want %q", cfg.TLSCertFile, tt.wantCert)
			}
			if cfg.TLSKeyFile != tt.wantKey {
				t.Fatalf("TLSKeyFile = %q, want %q", cfg.TLSKeyFile, tt.wantKey)
			}
			if cfg.LogHealthCheck != tt.wantLogHealth {
				t.Fatalf("LogHealthCheck = %v, want %v", cfg.LogHealthCheck, tt.wantLogHealth)
			}
		})
	}
}

func TestInitConfigAuditSettings(t *testing.T) {
	tests := []struct {
		name                string
		args                []string
		wantAuditEnabled    bool
		wantAuditFilePath   string
		wantAuditIncludeGet bool
		wantAuditMaxBody    int
		wantError           bool
	}{
		{
			name:                "default audit settings",
			args:                []string{},
			wantAuditEnabled:    false,
			wantAuditFilePath:   "",
			wantAuditIncludeGet: false,
			wantAuditMaxBody:    0,
			wantError:           false,
		},
		{
			name: "override audit settings",
			args: []string{
				"--audit-enabled",
				"--audit-file-path", "/var/log/cluster-agent/ops-audit.log",
				"--audit-include-get-requests",
				"--audit-max-body-size", "256",
			},
			wantAuditEnabled:    true,
			wantAuditFilePath:   "/var/log/cluster-agent/ops-audit.log",
			wantAuditIncludeGet: true,
			wantAuditMaxBody:    256,
			wantError:           false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			resetConfigForTest()
			t.Cleanup(resetConfigForTest)

			err := InitConfig(tt.args)
			if (err != nil) != tt.wantError {
				t.Fatalf("InitConfig() error = %v, wantError %v", err, tt.wantError)
			}

			cfg := GetConfig()
			if cfg == nil {
				t.Fatal("GetConfig() returned nil config")
			}

			if cfg.AuditEnabled != tt.wantAuditEnabled {
				t.Fatalf("AuditEnabled = %v, want %v", cfg.AuditEnabled, tt.wantAuditEnabled)
			}
			if cfg.AuditFilePath != tt.wantAuditFilePath {
				t.Fatalf("AuditFilePath = %q, want %q", cfg.AuditFilePath, tt.wantAuditFilePath)
			}
			if cfg.AuditIncludeGetRequests != tt.wantAuditIncludeGet {
				t.Fatalf("AuditIncludeGetRequests = %v, want %v", cfg.AuditIncludeGetRequests, tt.wantAuditIncludeGet)
			}
			if cfg.AuditMaxBodySize != tt.wantAuditMaxBody {
				t.Fatalf("AuditMaxBodySize = %d, want %d", cfg.AuditMaxBodySize, tt.wantAuditMaxBody)
			}
		})
	}
}
