package config

import (
	"testing"
	"time"
)

func TestLoadReadsRequestTimeout(t *testing.T) {
	t.Setenv("REQUEST_TIMEOUT_MS", "1200")

	cfg := Load()

	if cfg.RequestTimeout != 1200*time.Millisecond {
		t.Fatalf("RequestTimeout = %s, want 1.2s", cfg.RequestTimeout)
	}
}

func TestLoadReadsPlatformRelease(t *testing.T) {
	t.Setenv("PLATFORM_RELEASE", "platform-20260704153005")

	cfg := Load()

	if cfg.PlatformRelease != "platform-20260704153005" {
		t.Fatalf("PlatformRelease = %q", cfg.PlatformRelease)
	}
}

func TestLoadReadsAMapServerAPIKey(t *testing.T) {
	t.Setenv("AMAP_API_KEY", "server-map-key")

	cfg := Load()

	if cfg.AMapAPIKey != "server-map-key" {
		t.Fatalf("AMapAPIKey = %q", cfg.AMapAPIKey)
	}
}

func TestLoadSelectsProductionDataModeWhenMySQLIsConfigured(t *testing.T) {
	t.Setenv("DATA_MODE", "")
	t.Setenv("MYSQL_DSN", "user:password@tcp(db:3306)/platform")
	if cfg := Load(); cfg.DataMode != "production" {
		t.Fatalf("DataMode = %q, want production", cfg.DataMode)
	}
}

func TestLoadKeepsExplicitMockDataMode(t *testing.T) {
	t.Setenv("DATA_MODE", "mock")
	t.Setenv("MYSQL_DSN", "user:password@tcp(db:3306)/platform")
	if cfg := Load(); cfg.DataMode != "mock" {
		t.Fatalf("DataMode = %q, want mock", cfg.DataMode)
	}
}
