package recsettings

import (
	"context"
	"encoding/json"
	"errors"
	"io"
	"log/slog"
	"os"
	"testing"

	"github.com/jackc/pgx/v5/pgxpool"

	"git.fabledsword.com/bvandeusen/minstrel/internal/db"
	"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)

func newPool(t *testing.T) *pgxpool.Pool {
	t.Helper()
	if testing.Short() {
		t.Skip("skipping integration test in -short mode")
	}
	dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
	if dsn == "" {
		t.Skip("MINSTREL_TEST_DATABASE_URL not set")
	}
	logger := slog.New(slog.NewTextHandler(io.Discard, nil))
	if err := db.Migrate(dsn, logger); err != nil {
		t.Fatalf("migrate: %v", err)
	}
	pool, err := pgxpool.New(context.Background(), dsn)
	if err != nil {
		t.Fatalf("pool: %v", err)
	}
	t.Cleanup(pool.Close)
	dbtest.ResetDB(t, pool)
	return pool
}

func newService(t *testing.T, pool *pgxpool.Pool) *Service {
	t.Helper()
	s, err := New(context.Background(), pool, slog.New(slog.NewTextHandler(io.Discard, nil)))
	if err != nil {
		t.Fatalf("recsettings.New: %v", err)
	}
	return s
}

func auditRows(t *testing.T, pool *pgxpool.Pool) []struct {
	Scope, Action string
	Changes       []fieldChange
} {
	t.Helper()
	rows, err := pool.Query(context.Background(),
		`SELECT scope, action, changes FROM recommendation_tuning_audit ORDER BY id`)
	if err != nil {
		t.Fatalf("query audit: %v", err)
	}
	defer rows.Close()
	var out []struct {
		Scope, Action string
		Changes       []fieldChange
	}
	for rows.Next() {
		var scope, action string
		var raw []byte
		if err := rows.Scan(&scope, &action, &raw); err != nil {
			t.Fatalf("scan audit: %v", err)
		}
		var changes []fieldChange
		if err := json.Unmarshal(raw, &changes); err != nil {
			t.Fatalf("unmarshal audit changes: %v", err)
		}
		out = append(out, struct {
			Scope, Action string
			Changes       []fieldChange
		}{scope, action, changes})
	}
	return out
}

func TestNew_SeedsShippedDefaults(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	if got := s.Weights(ScopeRadio); got != ShippedRadioWeights() {
		t.Errorf("radio weights = %+v, want shipped defaults", got)
	}
	if got := s.Weights(ScopeDailyMix); got != ShippedDailyMixWeights() {
		t.Errorf("daily_mix weights = %+v, want shipped defaults", got)
	}
	if got := s.Taste(); got != ShippedTasteTuning() {
		t.Errorf("taste tuning = %+v, want shipped defaults", got)
	}
	// Seeding must not write audit rows — nothing changed.
	if rows := auditRows(t, pool); len(rows) != 0 {
		t.Errorf("boot reconcile wrote %d audit rows, want 0", len(rows))
	}
}

func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	if err := s.UpdateProfile(context.Background(), ScopeRadio,
		map[string]float64{"taste_weight": 2.5, "skip_penalty": 3.0}); err != nil {
		t.Fatalf("UpdateProfile: %v", err)
	}
	got := s.Weights(ScopeRadio)
	if got.TasteWeight != 2.5 || got.SkipPenalty != 3.0 {
		t.Errorf("weights = %+v, want taste 2.5 / skip 3.0", got)
	}
	// The other profile is untouched.
	if s.Weights(ScopeDailyMix) != ShippedDailyMixWeights() {
		t.Error("daily_mix must be unaffected by a radio update")
	}
	// Values survive a fresh boot (persisted, not just cached), and the
	// reconcile's ON CONFLICT DO NOTHING doesn't clobber tuned rows.
	s2 := newService(t, pool)
	if got := s2.Weights(ScopeRadio); got.TasteWeight != 2.5 {
		t.Errorf("rebooted taste_weight = %v, want 2.5", got.TasteWeight)
	}
	rows := auditRows(t, pool)
	if len(rows) != 1 {
		t.Fatalf("audit rows = %d, want 1", len(rows))
	}
	if rows[0].Scope != ScopeRadio || rows[0].Action != "update" || len(rows[0].Changes) != 2 {
		t.Errorf("audit row = %+v, want radio/update with 2 changes", rows[0])
	}
	// Changes are field-sorted: skip_penalty before taste_weight.
	if rows[0].Changes[0].Field != "skip_penalty" || rows[0].Changes[0].New != 3.0 {
		t.Errorf("changes[0] = %+v, want skip_penalty → 3.0", rows[0].Changes[0])
	}
}

func TestUpdateProfile_Validation(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	cases := []struct {
		name  string
		scope string
		patch map[string]float64
		want  error
	}{
		{"unknown scope", "banana", map[string]float64{"taste_weight": 1}, ErrUnknownScope},
		{"unknown field", ScopeRadio, map[string]float64{"vibes": 1}, ErrUnknownField},
		{"over bound", ScopeRadio, map[string]float64{"taste_weight": 11}, ErrOutOfRange},
		{"negative jitter", ScopeRadio, map[string]float64{"jitter_magnitude": -0.1}, ErrOutOfRange},
	}
	for _, c := range cases {
		if err := s.UpdateProfile(context.Background(), c.scope, c.patch); !errors.Is(err, c.want) {
			t.Errorf("%s: err = %v, want %v", c.name, err, c.want)
		}
	}
	if s.Weights(ScopeRadio) != ShippedRadioWeights() {
		t.Error("rejected patches must not partially apply")
	}
	if rows := auditRows(t, pool); len(rows) != 0 {
		t.Errorf("rejected patches wrote %d audit rows, want 0", len(rows))
	}
}

func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	// Moving neutral above full must reject on the PATCHED result.
	err := s.UpdateTaste(context.Background(), map[string]float64{"engagement_neutral": 0.95})
	if !errors.Is(err, ErrOutOfRange) {
		t.Fatalf("err = %v, want ErrOutOfRange (curve ordering)", err)
	}
	// A coherent multi-point move is fine.
	if err := s.UpdateTaste(context.Background(), map[string]float64{
		"engagement_neutral": 0.40, "engagement_full": 0.95, "half_life_days": 30,
	}); err != nil {
		t.Fatalf("UpdateTaste: %v", err)
	}
	cfg := s.TasteConfig()
	if cfg.HalfLifeDays != 30 || cfg.Engagement.NeutralCompletion != 0.40 {
		t.Errorf("taste config = %+v, want tuned values", cfg)
	}
	// WindowDays scales with the half-life at the shipped ratio.
	if cfg.WindowDays != 30*3.6 {
		t.Errorf("WindowDays = %v, want %v", cfg.WindowDays, 30*3.6)
	}
}

func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	// In-range update persists into cache + the assembled taste config.
	if err := s.UpdateTaste(context.Background(),
		map[string]float64{"enriched_tag_scale": 0.8}); err != nil {
		t.Fatalf("UpdateTaste: %v", err)
	}
	if got := s.Taste().EnrichedTagScale; got != 0.8 {
		t.Errorf("Taste().EnrichedTagScale = %v, want 0.8", got)
	}
	if got := s.TasteConfig().EnrichedTagScale; got != 0.8 {
		t.Errorf("TasteConfig().EnrichedTagScale = %v, want 0.8", got)
	}
	// Out of [0,1] rejects.
	if err := s.UpdateTaste(context.Background(),
		map[string]float64{"enriched_tag_scale": 1.5}); !errors.Is(err, ErrOutOfRange) {
		t.Errorf("err = %v, want ErrOutOfRange for 1.5", err)
	}
}

func TestUpdateTaste_EraScale(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	// In-range update persists into cache + the assembled taste config.
	if err := s.UpdateTaste(context.Background(),
		map[string]float64{"era_scale": 0.8}); err != nil {
		t.Fatalf("UpdateTaste: %v", err)
	}
	if got := s.Taste().EraScale; got != 0.8 {
		t.Errorf("Taste().EraScale = %v, want 0.8", got)
	}
	if got := s.TasteConfig().EraScale; got != 0.8 {
		t.Errorf("TasteConfig().EraScale = %v, want 0.8", got)
	}
	// Out of [0,1] rejects.
	if err := s.UpdateTaste(context.Background(),
		map[string]float64{"era_scale": 1.5}); !errors.Is(err, ErrOutOfRange) {
		t.Errorf("err = %v, want ErrOutOfRange for 1.5", err)
	}
}

func TestUpdateTaste_MoodScale(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	if err := s.UpdateTaste(context.Background(),
		map[string]float64{"mood_scale": 0.8}); err != nil {
		t.Fatalf("UpdateTaste: %v", err)
	}
	if got := s.Taste().MoodScale; got != 0.8 {
		t.Errorf("Taste().MoodScale = %v, want 0.8", got)
	}
	if got := s.TasteConfig().MoodScale; got != 0.8 {
		t.Errorf("TasteConfig().MoodScale = %v, want 0.8", got)
	}
	if err := s.UpdateTaste(context.Background(),
		map[string]float64{"mood_scale": 1.5}); !errors.Is(err, ErrOutOfRange) {
		t.Errorf("err = %v, want ErrOutOfRange for 1.5", err)
	}
}

func TestReset_RestoresShippedAndAudits(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	// Reset with nothing changed is a no-op (no audit row).
	if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
		t.Fatalf("no-op reset: %v", err)
	}
	if rows := auditRows(t, pool); len(rows) != 0 {
		t.Fatalf("no-op reset wrote audit rows")
	}
	if err := s.UpdateProfile(context.Background(), ScopeDailyMix,
		map[string]float64{"similarity_weight": 4}); err != nil {
		t.Fatalf("UpdateProfile: %v", err)
	}
	if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
		t.Fatalf("Reset: %v", err)
	}
	if s.Weights(ScopeDailyMix) != ShippedDailyMixWeights() {
		t.Error("reset must restore shipped defaults")
	}
	rows := auditRows(t, pool)
	if len(rows) != 2 || rows[1].Action != "reset" {
		t.Fatalf("audit rows = %+v, want update then reset", rows)
	}
}

func TestUpdate_NoOpWritesNoAudit(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	shipped := ShippedRadioWeights()
	if err := s.UpdateProfile(context.Background(), ScopeRadio,
		map[string]float64{"taste_weight": shipped.TasteWeight}); err != nil {
		t.Fatalf("UpdateProfile: %v", err)
	}
	if rows := auditRows(t, pool); len(rows) != 0 {
		t.Errorf("no-op update wrote %d audit rows, want 0", len(rows))
	}
}

// --- Discover scope (#2377, milestone #268 slice 6) ---

func TestNew_SeedsDiscoverDefaults(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	got := s.Discover()
	want := ShippedDiscoverTuning()
	if got != want {
		t.Errorf("Discover() = %+v, want shipped %+v", got, want)
	}
}

func TestUpdateDiscover_PersistsAndAudits(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	if err := s.UpdateDiscover(context.Background(), map[string]float64{
		"tag_overlap_weight": 2.5,
		"snooze_days":        30,
	}); err != nil {
		t.Fatalf("UpdateDiscover: %v", err)
	}
	if got := s.Discover().TagOverlapWeight; got != 2.5 {
		t.Errorf("TagOverlapWeight = %v, want 2.5", got)
	}
	if got := s.Discover().SnoozeDays; got != 30 {
		t.Errorf("SnoozeDays = %v, want 30", got)
	}

	// The audit row must land under the new scope. This is the assertion that
	// would have caught a missing rule-#36 CHECK migration: without expanding
	// recommendation_tuning_audit's whitelist, this INSERT fails at runtime.
	rows := auditRows(t, pool)
	if len(rows) != 1 {
		t.Fatalf("audit rows = %d, want 1", len(rows))
	}
	if rows[0].Scope != ScopeDiscover {
		t.Errorf("audit scope = %q, want %q", rows[0].Scope, ScopeDiscover)
	}
	if len(rows[0].Changes) != 2 {
		t.Errorf("audit changes = %+v, want both fields", rows[0].Changes)
	}

	// Reload from the DB to prove it persisted rather than only caching.
	s2 := newService(t, pool)
	if got := s2.Discover().TagOverlapWeight; got != 2.5 {
		t.Errorf("after reload TagOverlapWeight = %v, want 2.5 (not re-seeded to shipped)", got)
	}
}

func TestUpdateDiscover_Validation(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	cases := []struct {
		name  string
		patch map[string]float64
	}{
		{"unknown field", map[string]float64{"nope": 1}},
		{"negative weight", map[string]float64{"tag_overlap_weight": -1}},
		{"weight past the typo bound", map[string]float64{"tag_overlap_weight": 100}},
		// A sub-day snooze isn't a snooze, it's a flicker.
		{"snooze under a day", map[string]float64{"snooze_days": 0.5}},
		// Past a year it's a permanent dismissal wearing a snooze's clothes —
		// the shape rule #101 rules out.
		{"snooze past a year", map[string]float64{"snooze_days": 400}},
	}
	for _, c := range cases {
		if err := s.UpdateDiscover(context.Background(), c.patch); err == nil {
			t.Errorf("%s: expected rejection, got nil", c.name)
		}
	}
	if got := s.Discover(); got != ShippedDiscoverTuning() {
		t.Errorf("a rejected patch mutated state: %+v", got)
	}
	if rows := auditRows(t, pool); len(rows) != 0 {
		t.Errorf("rejected patches wrote %d audit rows, want 0", len(rows))
	}
}

// Weight 0 must be accepted — it's the operator's off switch for the whole
// tag term, so a "must be positive" bound would remove their ability to
// disable the feature.
func TestUpdateDiscover_ZeroWeightIsAllowed(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	if err := s.UpdateDiscover(context.Background(),
		map[string]float64{"tag_overlap_weight": 0}); err != nil {
		t.Fatalf("UpdateDiscover(0): %v", err)
	}
	if got := s.Discover().TagOverlapWeight; got != 0 {
		t.Errorf("TagOverlapWeight = %v, want 0", got)
	}
}

func TestResetDiscover_RestoresShippedDefaults(t *testing.T) {
	pool := newPool(t)
	s := newService(t, pool)
	if err := s.UpdateDiscover(context.Background(),
		map[string]float64{"tag_overlap_weight": 4}); err != nil {
		t.Fatalf("UpdateDiscover: %v", err)
	}
	if err := s.Reset(context.Background(), ScopeDiscover); err != nil {
		t.Fatalf("Reset: %v", err)
	}
	if got := s.Discover(); got != ShippedDiscoverTuning() {
		t.Errorf("after reset = %+v, want shipped %+v", got, ShippedDiscoverTuning())
	}
	// Already-at-defaults is a no-op: update + reset = 2 rows, not 3.
	if err := s.Reset(context.Background(), ScopeDiscover); err != nil {
		t.Fatalf("second Reset: %v", err)
	}
	if rows := auditRows(t, pool); len(rows) != 2 {
		t.Errorf("audit rows = %d, want 2 (the no-op reset must not audit)", len(rows))
	}
}
