package service

import (
	"encoding/json"
	"strconv"
	"testing"

	"github.com/alicebob/miniredis/v2"
	"github.com/ez-api/ez-api/internal/model"
	"github.com/redis/go-redis/v9"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type bindingSnapshot struct {
	Namespace   string `json:"namespace"`
	PublicModel string `json:"public_model"`
	Candidates  []struct {
		RouteGroup string            `json:"route_group"`
		Error      string            `json:"error,omitempty"`
		Upstreams  map[string]string `json:"upstreams"`
	} `json:"candidates"`
}

func TestSyncBindings_SelectorExact(t *testing.T) {
	t.Parallel()

	db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "m", Status: "active"}
	if err := db.Create(&group).Error; err != nil {
		t.Fatalf("create group: %v", err)
	}
	key := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
	if err := db.Create(&key).Error; err != nil {
		t.Fatalf("create api key: %v", err)
	}
	b := model.Binding{Namespace: "ns", PublicModel: "m", GroupID: group.ID, Weight: 1, SelectorType: "exact", Status: "active"}
	if err := db.Create(&b).Error; err != nil {
		t.Fatalf("create binding: %v", err)
	}

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	svc := NewSyncService(rdb)
	if err := svc.SyncBindings(db); err != nil {
		t.Fatalf("SyncBindings: %v", err)
	}

	raw := mr.HGet("config:bindings", "ns.m")
	if raw == "" {
		t.Fatalf("expected config:bindings entry")
	}
	var snap bindingSnapshot
	if err := json.Unmarshal([]byte(raw), &snap); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if len(snap.Candidates) != 1 {
		t.Fatalf("expected 1 candidate, got %+v", snap.Candidates)
	}
	if snap.Candidates[0].Upstreams == nil || snap.Candidates[0].Upstreams[jsonID(key.ID)] != "m" {
		t.Fatalf("unexpected upstreams: %+v", snap.Candidates[0].Upstreams)
	}
}

func TestSyncBindings_SelectorRegexAndNormalize(t *testing.T) {
	t.Parallel()

	db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "moonshot/kimi2,kimi2", Status: "active"}
	if err := db.Create(&group).Error; err != nil {
		t.Fatalf("create group: %v", err)
	}
	k1 := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
	k2 := model.APIKey{GroupID: group.ID, APIKey: "k2", Status: "active"}
	if err := db.Create(&k1).Error; err != nil {
		t.Fatalf("create api key1: %v", err)
	}
	if err := db.Create(&k2).Error; err != nil {
		t.Fatalf("create api key2: %v", err)
	}

	// Regex should match uniquely (moonshot/kimi2 only).
	bRegex := model.Binding{Namespace: "ns", PublicModel: "kimi2", GroupID: group.ID, Weight: 1, SelectorType: "regex", SelectorValue: "^moonshot/kimi2$", Status: "active"}
	if err := db.Create(&bRegex).Error; err != nil {
		t.Fatalf("create binding regex: %v", err)
	}

	// Normalize_exact should match p2 (moonshot/kimi2) for "kimi2".
	bNorm := model.Binding{Namespace: "ns", PublicModel: "kimi2-n", GroupID: group.ID, Weight: 1, SelectorType: "normalize_exact", SelectorValue: "kimi2", Status: "active"}
	if err := db.Create(&bNorm).Error; err != nil {
		t.Fatalf("create binding normalize: %v", err)
	}

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	svc := NewSyncService(rdb)
	if err := svc.SyncBindings(db); err != nil {
		t.Fatalf("SyncBindings: %v", err)
	}

	// Regex binding should include both providers mapped to moonshot/kimi2 (p1 also has it).
	raw := mr.HGet("config:bindings", "ns.kimi2")
	var snapRegex bindingSnapshot
	if err := json.Unmarshal([]byte(raw), &snapRegex); err != nil {
		t.Fatalf("unmarshal regex: %v", err)
	}
	if len(snapRegex.Candidates) != 1 {
		t.Fatalf("expected 1 candidate, got %+v", snapRegex.Candidates)
	}
	upstreams := snapRegex.Candidates[0].Upstreams
	if upstreams[jsonID(k1.ID)] != "moonshot/kimi2" || upstreams[jsonID(k2.ID)] != "moonshot/kimi2" {
		t.Fatalf("unexpected regex upstreams: %+v", upstreams)
	}

	// Normalize_exact binding should include p2 but exclude p1 due to multi-match (moonshot/kimi2 + kimi2).
	raw = mr.HGet("config:bindings", "ns.kimi2-n")
	var snapNorm bindingSnapshot
	if err := json.Unmarshal([]byte(raw), &snapNorm); err != nil {
		t.Fatalf("unmarshal normalize: %v", err)
	}
	if len(snapNorm.Candidates) != 1 {
		t.Fatalf("expected 1 candidate, got %+v", snapNorm.Candidates)
	}
	if len(snapNorm.Candidates[0].Upstreams) != 0 || snapNorm.Candidates[0].Error != "config_error" {
		t.Fatalf("expected config_error with no upstreams, got %+v", snapNorm.Candidates[0])
	}
}

func TestSyncBindings_VersionChangesWithinSameSecond(t *testing.T) {
	t.Parallel()

	db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "m", Status: "active"}
	if err := db.Create(&group).Error; err != nil {
		t.Fatalf("create group: %v", err)
	}
	key := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
	if err := db.Create(&key).Error; err != nil {
		t.Fatalf("create api key: %v", err)
	}
	b := model.Binding{Namespace: "ns", PublicModel: "m", GroupID: group.ID, Weight: 1, SelectorType: "exact", Status: "active"}
	if err := db.Create(&b).Error; err != nil {
		t.Fatalf("create binding: %v", err)
	}

	mr := miniredis.RunT(t)
	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	svc := NewSyncService(rdb)

	var prevVersion string
	var prevUpdated string
	for i := 0; i < 10; i++ {
		if err := svc.SyncBindings(db); err != nil {
			t.Fatalf("SyncBindings: %v", err)
		}
		version := mr.HGet("meta:bindings_meta", "version")
		updated := mr.HGet("meta:bindings_meta", "updated_at")
		if version == "" || updated == "" {
			t.Fatalf("expected bindings meta fields, got version=%q updated_at=%q", version, updated)
		}
		if _, err := strconv.ParseInt(version, 10, 64); err != nil {
			t.Fatalf("invalid version: %q", version)
		}
		if _, err := strconv.ParseInt(updated, 10, 64); err != nil {
			t.Fatalf("invalid updated_at: %q", updated)
		}

		if prevUpdated != "" && updated == prevUpdated {
			if version == prevVersion {
				t.Fatalf("expected version to change within same second, got %q", version)
			}
			return
		}
		prevVersion = version
		prevUpdated = updated
	}
	t.Fatalf("failed to observe two syncs within the same second")
}
