package store

import (
	"io"
	"log"
	"path/filepath"
	"testing"
)

func newFloorTestStore(t *testing.T) *Store {
	t.Helper()
	s, err := New(filepath.Join(t.TempDir(), "floor.db"), log.New(io.Discard, "", 0))
	if err != nil {
		t.Fatalf("store.New: %v", err)
	}
	t.Cleanup(func() { s.Close() })
	return s
}

func mkCustomer(t *testing.T, s *Store, id, floor string) {
	t.Helper()
	if err := s.SaveCustomerConfig(&CustomerConfig{
		CustomerID:           id,
		RetrievalPassword:    "pw",
		APIKey:               "key-" + id,
		ConfigJSON:           "{}",
		MinControllerVersion: floor,
	}); err != nil {
		t.Fatalf("SaveCustomerConfig(%s): %v", id, err)
	}
}

// EffectiveMinControllerVersion: empty when nothing is set.
func TestEffectiveFloor_EmptyWhenUnset(t *testing.T) {
	s := newFloorTestStore(t)
	mkCustomer(t, s, "c", "")
	if got := s.EffectiveMinControllerVersion("c"); got != "" {
		t.Errorf("effective floor = %q, want empty (no override, no global)", got)
	}
	// Also empty for a customer with no config at all.
	if got := s.EffectiveMinControllerVersion("ghost"); got != "" {
		t.Errorf("effective floor for unknown customer = %q, want empty", got)
	}
}

// The config/env default is the fallback global floor.
func TestEffectiveFloor_GlobalDefaultFallback(t *testing.T) {
	s := newFloorTestStore(t)
	s.SetDefaultMinControllerVersion("0.85.0")
	mkCustomer(t, s, "c", "")
	if got := s.EffectiveMinControllerVersion("c"); got != "0.85.0" {
		t.Errorf("effective floor = %q, want 0.85.0 (global default)", got)
	}
}

// A hub_settings row overrides the config/env default.
func TestGlobalFloor_DBOverridesDefault(t *testing.T) {
	s := newFloorTestStore(t)
	s.SetDefaultMinControllerVersion("0.85.0")
	if err := s.SetGlobalMinControllerVersion("0.86.0"); err != nil {
		t.Fatalf("SetGlobalMinControllerVersion: %v", err)
	}
	if got := s.GetGlobalMinControllerVersion(); got != "0.86.0" {
		t.Errorf("global floor = %q, want 0.86.0 (DB beats default)", got)
	}
	// Clearing the row falls back to the default.
	if err := s.SetGlobalMinControllerVersion(""); err != nil {
		t.Fatalf("clear global floor: %v", err)
	}
	if got := s.GetGlobalMinControllerVersion(); got != "0.85.0" {
		t.Errorf("global floor after clear = %q, want 0.85.0 (default)", got)
	}
}

// Scenario C — per-customer override beats global. This is the companion RED-PROOF: it must FAIL if
// EffectiveMinControllerVersion ever returns the global when an override is set.
func TestEffectiveFloor_OverrideBeatsGlobal(t *testing.T) {
	s := newFloorTestStore(t)
	s.SetDefaultMinControllerVersion("0.85.0")        // global
	mkCustomer(t, s, "c", "0.87.0")                   // per-customer override
	mkCustomer(t, s, "other", "")                     // no override → uses global

	if got := s.EffectiveMinControllerVersion("c"); got != "0.87.0" {
		t.Errorf("effective floor for c = %q, want 0.87.0 (override beats global)", got)
	}
	if got := s.EffectiveMinControllerVersion("other"); got != "0.85.0" {
		t.Errorf("effective floor for other = %q, want 0.85.0 (global, no override)", got)
	}
}

// SetMinControllerVersion round-trips and is preserved across an unrelated SaveCustomerConfig.
func TestSetMinControllerVersion_PreservedOnSave(t *testing.T) {
	s := newFloorTestStore(t)
	mkCustomer(t, s, "c", "")
	if err := s.SetMinControllerVersion("c", "0.88.0"); err != nil {
		t.Fatalf("SetMinControllerVersion: %v", err)
	}
	// A typical edit path: load, mutate an unrelated field, save.
	cfg, _ := s.GetCustomerConfig("c")
	if cfg.MinControllerVersion != "0.88.0" {
		t.Fatalf("after Set, MinControllerVersion = %q, want 0.88.0", cfg.MinControllerVersion)
	}
	cfg.Email = "x@example.com"
	if err := s.SaveCustomerConfig(cfg); err != nil {
		t.Fatalf("SaveCustomerConfig: %v", err)
	}
	cfg2, _ := s.GetCustomerConfig("c")
	if cfg2.MinControllerVersion != "0.88.0" {
		t.Errorf("floor lost across save: %q, want 0.88.0", cfg2.MinControllerVersion)
	}
}
