package config

import (
	"os"
	"path/filepath"
	"testing"
	"time"
)

type testConfig struct {
	Name string `yaml:"name"`
}

func (c *testConfig) Validate() error {
	if c.Name == "" {
		return os.ErrInvalid
	}
	return nil
}

func TestLoadYAMLValidatesConfig(t *testing.T) {
	path := filepath.Join(t.TempDir(), "config.yaml")
	if err := os.WriteFile(path, []byte("name: activity-service\n"), 0o644); err != nil {
		t.Fatalf("write config failed: %v", err)
	}
	var cfg testConfig
	if err := LoadYAML(path, &cfg); err != nil {
		t.Fatalf("load yaml failed: %v", err)
	}
	if cfg.Name != "activity-service" {
		t.Fatalf("unexpected name: %q", cfg.Name)
	}
}

func TestEnvOverrides(t *testing.T) {
	t.Setenv("SERVICEKIT_STRING", "  value  ")
	t.Setenv("SERVICEKIT_BOOL", "true")
	t.Setenv("SERVICEKIT_DURATION", "3s")

	text := ""
	enabled := false
	delay := time.Duration(0)
	OverrideStringFromEnv(&text, "SERVICEKIT_STRING")
	if err := OverrideBoolFromEnv(&enabled, "SERVICEKIT_BOOL"); err != nil {
		t.Fatalf("bool override failed: %v", err)
	}
	if err := OverrideDurationFromEnv(&delay, "SERVICEKIT_DURATION"); err != nil {
		t.Fatalf("duration override failed: %v", err)
	}
	if text != "value" || !enabled || delay != 3*time.Second {
		t.Fatalf("env overrides mismatch: text=%q enabled=%t delay=%s", text, enabled, delay)
	}
}
